-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(cn) translate Learn/removing effect dependencies into Chinese #1194
docs(cn) translate Learn/removing effect dependencies into Chinese #1194
Conversation
Size changes📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖 Three Pages Changed SizeThe following pages changed size from the code in this PR compared to its base branch:
DetailsOnly the gzipped size is provided here based on an expert tip. First Load is the size of the global bundle plus the bundle for the individual page. If a user were to show up to your website and land on a given page, the first load size represents the amount of javascript that user would need to download. If Any third party scripts you have added directly to your app using the Next to the size is how much the size has increased or decreased compared with the base branch of this PR. If this percentage has increased by 10% or more, there will be a red status indicator applied, indicating that special attention should be given to this. |
// ... | ||
} | ||
``` | ||
|
||
[Effects "react" to reactive values.](/learn/lifecycle-of-reactive-effects#effects-react-to-reactive-values) Since `roomId` is a reactive value (it can change due to a re-render), the linter verifies that you've specified it as a dependency. If `roomId` receives a different value, React will re-synchronize your Effect. This ensures that the chat stays connected to the selected room and "reacts" to the dropdown: | ||
[Effect 会对响应式值“做出响应”](/learn/lifecycle-of-reactive-effects#effects-react-to-reactive-values)。由于 roomId 是响应式值(它会因为重新渲染而变化),代码检查工具会验证你是否已经将它指定为依赖项。每当 roomId 接收到一个不同的值,React 就会重新同步对应的 Effect。这可以保证聊天会一直和选中的房间保持连接,并对下拉框的变化“做出响应”: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Effect 会对响应式值“做出响应”](/learn/lifecycle-of-reactive-effects#effects-react-to-reactive-values)。由于 roomId 是响应式值(它会因为重新渲染而变化),代码检查工具会验证你是否已经将它指定为依赖项。每当 roomId 接收到一个不同的值,React 就会重新同步对应的 Effect。这可以保证聊天会一直和选中的房间保持连接,并对下拉框的变化“做出响应”: | |
[Effect 会对响应式值“做出响应”](/learn/lifecycle-of-reactive-effects#effects-react-to-reactive-values)。由于 `roomId` 是响应式值(它会因为重新渲染而变化),代码检查工具会验证你是否已经将它指定为依赖项。每当 `roomId` 接收到一个不同的值,React 就会重新同步对应的 Effect。这可以保证聊天会一直和选中的房间保持连接,并对下拉框的变化“做出响应”: |
@@ -263,43 +263,43 @@ button { margin-left: 10px; } | |||
|
|||
</Sandpack> | |||
|
|||
This is why you could now specify an [empty (`[]`) dependency list.](/learn/lifecycle-of-reactive-effects#what-an-effect-with-empty-dependencies-means) Your Effect *really doesn't* depend on any reactive value anymore, so it *really doesn't* need to re-run when any of the component's props or state change. | |||
这就是为什么你现在可以指定一个 [空 (`[]`) 依赖项列表](/learn/lifecycle-of-reactive-effects#what-an-effect-with-empty-dependencies-means)。Effect **实际上不再**依赖任何响应式值,所以当组件的任何 props 和 state 变化时,它**并不**需要重新运行。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这就是为什么你现在可以指定一个 [空 (`[]`) 依赖项列表](/learn/lifecycle-of-reactive-effects#what-an-effect-with-empty-dependencies-means)。Effect **实际上不再**依赖任何响应式值,所以当组件的任何 props 和 state 变化时,它**并不**需要重新运行。 | |
这就是为什么你现在可以指定一个 [空 (`[]`) 依赖项列表](/learn/lifecycle-of-reactive-effects#what-an-effect-with-empty-dependencies-means)。Effect **实际上不再** 依赖任何响应式值,所以当组件的任何 props 和 state 变化时,它 **并不** 需要重新运行。 |
|
||
There's always a better solution than ignoring the linter! To fix this code, you need to add `onTick` to the dependency list. (To ensure the interval is only setup once, [make `onTick` an Effect Event.](/learn/separating-events-from-effects#reading-latest-props-and-state-with-effect-events)) | ||
比起忽略代码检查,有一个更好的解决方法!你需要向依赖项列表中加入 `onTick` 来修复这段代码。(为了确保 interval 只设置一次,需要 [让 `onTick` 成为 Effect Event](/learn/separating-events-from-effects#reading-latest-props-and-state-with-effect-events)。) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
考虑一下此处的 Event 是否需要翻译
@@ -435,13 +435,13 @@ function Form() { | |||
} | |||
``` | |||
|
|||
Now that the code is in an event handler, it's not reactive--so it will only run when the user submits the form. Read more about [choosing between event handlers and Effects](/learn/separating-events-from-effects#reactive-values-and-reactive-logic) and [how to delete unnecessary Effects.](/learn/you-might-not-need-an-effect) | |||
既然代码是在一个事件处理函数里,所以它不是响应式的 — 所以它只会在用户提交表单的时候运行。了解更多关于 [如何选择事件处理函数和 Effect](/learn/separating-events-from-effects#reactive-values-and-reactive-logic) 以及 [如何删除不必要的Effect](/learn/you-might-not-need-an-effect)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
既然代码是在一个事件处理函数里,所以它不是响应式的 — 所以它只会在用户提交表单的时候运行。了解更多关于 [如何选择事件处理函数和 Effect](/learn/separating-events-from-effects#reactive-values-and-reactive-logic) 以及 [如何删除不必要的Effect](/learn/you-might-not-need-an-effect)。 | |
既然代码是在一个事件处理函数里,所以它不是响应式的——所以它只会在用户提交表单的时候运行。了解更多关于 [如何选择事件处理函数和 Effect](/learn/separating-events-from-effects#reactive-values-and-reactive-logic) 以及 [如何删除不必要的Effect](/learn/you-might-not-need-an-effect)。 |
|
||
The final code is longer than the original, but splitting these Effects is still correct. [Each Effect should represent an independent synchronization process.](/learn/lifecycle-of-reactive-effects#each-effect-represents-a-separate-synchronization-process) In this example, deleting one Effect doesn't break the other Effect's logic. This means they *synchronize different things,* and it's good to split them up. If you're concerned about duplication, you can improve this code by [extracting repetitive logic into a custom Hook.](/learn/reusing-logic-with-custom-hooks#when-to-use-custom-hooks) | ||
最终代码比原本的代码更长,但是分割这些 Effect 的做法仍然是非常正确的。[每个 Effect 应该表示一个独立的同步进程](/learn/lifecycle-of-reactive-effects#each-effect-represents-a-separate-synchronization-process)。在这个例子中,删除一个 Effect 不会破坏其他 Effect 的逻辑。这意味着他们 **同步不同的事物**,并且拆分它们是有好处的。如果你担心重复,可以通过 [提取重复逻辑到自定义 Hook ](/learn/reusing-logic-with-custom-hooks#when-to-use-custom-hooks)来改进这段代码。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最终代码比原本的代码更长,但是分割这些 Effect 的做法仍然是非常正确的。[每个 Effect 应该表示一个独立的同步进程](/learn/lifecycle-of-reactive-effects#each-effect-represents-a-separate-synchronization-process)。在这个例子中,删除一个 Effect 不会破坏其他 Effect 的逻辑。这意味着他们 **同步不同的事物**,并且拆分它们是有好处的。如果你担心重复,可以通过 [提取重复逻辑到自定义 Hook ](/learn/reusing-logic-with-custom-hooks#when-to-use-custom-hooks)来改进这段代码。 | |
最终代码比原本的代码更长,但是分割这些 Effect 的做法仍然是非常正确的。[每个 Effect 应该表示一个独立的同步进程](/learn/lifecycle-of-reactive-effects#each-effect-represents-a-separate-synchronization-process)。在这个例子中,删除一个 Effect 不会破坏其他 Effect 的逻辑。这意味着他们 **同步不同的事物**,并且拆分它们是有好处的。如果你担心重复,可以通过 [提取重复逻辑到自定义 Hook](/learn/reusing-logic-with-custom-hooks#when-to-use-custom-hooks) 来改进这段代码。 |
有一些格式上的小问题,烦请自查一遍~ |
另外,此篇翻译与此 PR 对应翻译重复。 |
当出现重复翻译时,我的想法是: |
好的,我会关闭这个PR,并根据此翻译内容review另外一个PR |
@Yucohny 已完成,等待校对