You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
React is responsible for rendering components and Hooks when necessary to optimize the user experience. It is declarative: you tell React what to render in your component’s logic, and React will figure out how best to display it to your user.
React must decide when your component function is called [during rendering](/reference/rules/components-and-hooks-must-be-pure#how-does-react-run-your-code). In React, you do this using JSX.
If a component contains Hooks, it's easy to violate the [Rules of Hooks](/reference/rules/rules-of-hooks) when components are called directly in a loop or conditionally.
Letting React orchestrate rendering also allows a number of benefits:
32
+
让 React 来协调渲染还有许多好处:
33
33
34
-
***Components become more than functions.** React can augment them with features like _local state_ through Hooks that are tied to the component's identity in the tree.
35
-
***Component types participate in reconciliation.** By letting React call your components, you also tell it more about the conceptual structure of your tree. For example, when you move from rendering `<Feed>`to the `<Profile>`page, React won’t attempt to re-use them.
36
-
***React can enhance your user experience.** For example, it can let the browser do some work between component calls so that re-rendering a large component tree doesn’t block the main thread.
37
-
***A better debugging story.** If components are first-class citizens that the library is aware of, we can build rich developer tools for introspection in development.
38
-
***More efficient reconciliation.** React can decide exactly which components in the tree need re-rendering and skip over the ones that don't. That makes your app faster and more snappy.
Hooks should only be called inside of components or Hooks. Never pass it around as a regular value.
44
+
Hook 只能在组件或 Hook 内部调用。永远不要像常规值一样传递它们。
45
45
46
-
Hooks allow you to augment a component with React features. They should always be called as a function, and never passed around as a regular value. This enables _local reasoning_, or the ability for developers to understand everything a component can do by looking at that component in isolation.
Hooks should be as "static" as possible. This means you shouldn't dynamically mutate them. For example, this means you shouldn't write higher order Hooks:
Hooks should be immutable and not be mutated. Instead of mutating a Hook dynamically, create a static version of the Hook with the desired functionality.
@@ -70,17 +70,17 @@ function useDataWithLogging() {
70
70
}
71
71
```
72
72
73
-
### Don't dynamically use Hooks {/*dont-dynamically-use-hooks*/}
73
+
### 不要动态地使用 Hook {/*dont-dynamically-use-hooks*/}
74
74
75
-
Hooks should also not be dynamically used: for example, instead of doing dependency injection in a component by passing a Hook as a value:
75
+
Hook 也不应该被动态使用,例如,不应该通过将 Hook 作为值传递来在一个组件中实现依赖注入。
76
76
77
77
```js {2}
78
78
functionChatInput() {
79
79
return<Button useData={useDataWithLogging} />// 🔴 Bad: don't pass Hooks as props
80
80
}
81
81
```
82
82
83
-
You should always inline the call of the Hook into that component and handle any logic in there.
83
+
你应该始终将 Hook 的调用内联到组件内部,并在其中处理所有逻辑。
84
84
85
85
```js {6}
86
86
functionChatInput() {
@@ -97,5 +97,5 @@ function useDataWithLogging() {
97
97
}
98
98
```
99
99
100
-
This way, `<Button />`is much easier to understand and debug. When Hooks are used in dynamic ways, it increases the complexity of your app greatly and inhibits local reasoning, making your team less productive in the long term. It also makes it easier to accidentally break the [Rules of Hooks](/reference/rules/rules-of-hooks) that Hooks should not be called conditionally. If you find yourself needing to mock components for tests, it's better to mock the server instead to respond with canned data. If possible, it's also usually more effective to test your app with end-to-end tests.
0 commit comments