@@ -17,13 +17,13 @@ React 必须决定 [在渲染过程中](/reference/rules/components-and-hooks-mu
1717
1818``` js {2}
1919function BlogPost () {
20- return < Layout>< Article / >< / Layout> ; // ✅ Good: Only use components in JSX
20+ return < Layout>< Article / >< / Layout> ; // ✅ 正确的:只在 JSX 中使用组件
2121}
2222```
2323
2424``` js {2}
2525function BlogPost () {
26- return < Layout> {Article ()}< / Layout> ; // 🔴 Bad: Never call them directly
26+ return < Layout> {Article ()}< / Layout> ; // 🔴 错误的:不要直接调用组件函数
2727}
2828```
2929
@@ -53,7 +53,7 @@ Hook 应当尽可能保持“静态”。这意味着你不应该动态地改变
5353
5454``` js {2}
5555function ChatInput () {
56- const useDataWithLogging = withLogging (useData); // 🔴 Bad: don't write higher order Hooks
56+ const useDataWithLogging = withLogging (useData); // 🔴 错误的:不要编写高阶 Hook
5757 const data = useDataWithLogging ();
5858}
5959```
@@ -62,11 +62,11 @@ Hook 应该是不可变的,不应被动态改变。与其动态地改变 Hook
6262
6363``` js {2,6}
6464function ChatInput () {
65- const data = useDataWithLogging (); // ✅ Good: Create a new version of the Hook
65+ const data = useDataWithLogging (); // ✅ 正确的:创建一个新的 Hook
6666}
6767
6868function useDataWithLogging () {
69- // ... Create a new version of the Hook and inline the logic here
69+ // ... 创建一个新的 Hook,并将逻辑直接内联到这里
7070}
7171```
7272
@@ -76,7 +76,7 @@ Hook 也不应该被动态使用,例如,不应该通过将 Hook 作为值传
7676
7777``` js {2}
7878function ChatInput () {
79- return < Button useData= {useDataWithLogging} / > // 🔴 Bad: don't pass Hooks as props
79+ return < Button useData= {useDataWithLogging} / > // 🔴 错误的:不要通过 props 传递 Hook
8080}
8181```
8282
@@ -88,14 +88,12 @@ function ChatInput() {
8888}
8989
9090function Button () {
91- const data = useDataWithLogging (); // ✅ Good: Use the Hook directly
91+ const data = useDataWithLogging (); // ✅ 正确的:直接使用 Hook
9292}
9393
9494function useDataWithLogging () {
95- // If there's any conditional logic to change the Hook's behavior, it should be inlined into
96- // the Hook
95+ // 如果有任何条件逻辑需要改变 Hook 的行为,应将其直接内联到 Hook 中
9796}
9897```
9998
10099这样,` <Button /> ` 组件更容易理解也更易于调试。当 Hook 以动态方式使用时,会大大增加应用的复杂性,并妨碍局部推理,这从长远来看会降低团队的生产力。它还更容易意外地违反 [ Hook 的规则] ( /reference/rules/rules-of-hooks ) ,即 Hook 不应该被条件性地调用。如果你发现自己需要为测试而模拟组件,最好是模拟服务器返回以响应预设数据。如果可能,通常进行端到端测试你的应用是更有效的方法。
101-
0 commit comments