Skip to content

Commit 999bf2a

Browse files
committed
docs(reference/react): translate ref callback and partial-app StrictMode content to Chinese
1 parent 4ad00a4 commit 999bf2a

1 file changed

Lines changed: 31 additions & 31 deletions

File tree

src/content/reference/react/StrictMode.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function App() {
124124

125125
<Note>
126126

127-
When `StrictMode` is enabled for a part of the app, React will only enable behaviors that are possible in production. For example, if `<StrictMode>` is not enabled at the root of the app, it will not [re-run Effects an extra time](#fixing-bugs-found-by-re-running-effects-in-development) on initial mount, since this would cause child effects to double fire without the parent effects, which cannot happen in production.
127+
`StrictMode` 仅为应用程序的一部分启用时,React 仅会启用在生产环境中可能发生的行为。例如,如果 `<StrictMode>` 未在应用根节点启用,则在初始挂载时它不会[额外重新运行 Effect](#fixing-bugs-found-by-re-running-effects-in-development),因为这会导致子 Effect 在没有父 Effect 的情况下触发两次,而这在生产环境中是不可能发生的。
128128

129129
</Note>
130130

@@ -319,7 +319,7 @@ li {
319319
export default function StoryTray({ stories }) {
320320
const items = stories.slice(); // 复制数组
321321
// ✅ 正确的:在新数组上进行修改
322-
items.push({ id: 'create', label: 'Create Story' });
322+
items.push({ id: 'create', label: '创建故事' });
323323
```
324324
325325
这样做会 [使 `StoryTray` 函数成为纯函数](/learn/keeping-components-pure)。每次调用函数时,它只会修改一个新的数组副本,不会影响任何外部对象或变量。这解决了错误,但在发现其行为有问题之前,你可能需要更频繁地使组件重新渲染。
@@ -833,15 +833,15 @@ button { margin-left: 10px; }
833833
[请阅读更多关于实现 Effect 清理的内容](/learn/synchronizing-with-effects#how-to-handle-the-effect-firing-twice-in-development)。
834834
835835
---
836-
### Fixing bugs found by re-running ref callbacks in development {/*fixing-bugs-found-by-re-running-ref-callbacks-in-development*/}
836+
### 修复在开发中通过重新运行 ref 回调发现的错误 {/*fixing-bugs-found-by-re-running-ref-callbacks-in-development*/}
837837
838-
Strict Mode can also help find bugs in [callbacks refs.](/learn/manipulating-the-dom-with-refs)
838+
严格模式也可以帮助发现[回调 ref](/learn/manipulating-the-dom-with-refs) 中的错误。
839839
840-
Every callback `ref` has some setup code and may have some cleanup code. Normally, React calls setup when the element is *created* (is added to the DOM) and calls cleanup when the element is *removed* (is removed from the DOM).
840+
每个回调 `ref` 都有一些 setup 代码,以及可能的 cleanup 代码。通常,当元素被*创建*(添加到 DOM)时,React 会调用 setup;当元素被*移除*(从 DOM 中移除)时,React 会调用 cleanup
841841
842-
When Strict Mode is on, React will also run **one extra setup+cleanup cycle in development for every callback `ref`.** This may feel surprising, but it helps reveal subtle bugs that are hard to catch manually.
842+
当开启严格模式时,React 还会在开发模式下为每个回调 `ref` **额外运行一次 setup+cleanup 循环**。这可能会让人感到惊讶,但它有助于发现手动难以捕捉到的细微错误。
843843
844-
Consider this example, which allows you to select an animal and then scroll to one of them. Notice when you switch from "Cats" to "Dogs", the console logs show that the number of animals in the list keeps growing, and the "Scroll to" buttons stop working:
844+
参考以下示例,它允许你选择一种动物然后滚动到其中之一。注意,当你从 "Cats" 切换到 "Dogs" 时,控制台日志显示列表中的动物数量持续增长,而「滚动到」按钮停止工作:
845845
846846
<Sandpack>
847847
@@ -852,7 +852,7 @@ import './styles.css';
852852
import App from './App';
853853

854854
const root = createRoot(document.getElementById("root"));
855-
//Not using StrictMode.
855+
//未使用 StrictMode
856856
root.render(<App />);
857857
```
858858
@@ -884,7 +884,7 @@ export default function CatFriends() {
884884
</nav>
885885
<hr />
886886
<nav>
887-
<span>Scroll to:</span>{cats.map((cat, index) => (
887+
<span>滚动到:</span>{cats.map((cat, index) => (
888888
<button key={cat.src} onClick={() => scrollToCat(index)}>
889889
{index}
890890
</button>
@@ -899,12 +899,12 @@ export default function CatFriends() {
899899
const list = itemsRef.current;
900900
const item = {cat: cat, node};
901901
list.push(item);
902-
console.log(`Adding cat to the map. Total cats: ${list.length}`);
902+
console.log(`将猫添加到 map 中。当前猫的总数:${list.length}`);
903903
if (list.length > 10) {
904-
console.log('Too many cats in the list!');
904+
console.log('列表中的猫太多了!');
905905
}
906906
return () => {
907-
// 🚩 No cleanup, this is a bug!
907+
// 🚩 没有清理,这是一个错误!
908908
}
909909
}}
910910
>
@@ -960,9 +960,9 @@ li {
960960
</Sandpack>
961961
962962
963-
**This is a production bug!** Since the ref callback doesn't remove animals from the list in the cleanup, the list of animals keeps growing. This is a memory leak that can cause performance problems in a real app, and breaks the behavior of the app.
963+
**这是一个生产环境的错误!** 由于 ref 回调没有在清理时从列表中移除动物,动物列表会持续增长。这是一处内存泄漏,会在实际应用中引发性能问题,并破坏应用的行为。
964964
965-
The issue is the ref callback doesn't cleanup after itself:
965+
问题在于 ref 回调没有在自身之后进行清理:
966966
967967
```js {6-8}
968968
<li
@@ -971,13 +971,13 @@ The issue is the ref callback doesn't cleanup after itself:
971971
const item = {animal, node};
972972
list.push(item);
973973
return () => {
974-
// 🚩 No cleanup, this is a bug!
974+
// 🚩 没有清理,这是一个错误!
975975
}
976976
}}
977977
</li>
978978
```
979979
980-
Now let's wrap the original (buggy) code in `<StrictMode>`:
980+
现在让我们将原始(有错误的)代码包裹在 `<StrictMode>` 中:
981981
982982
<Sandpack>
983983
@@ -989,7 +989,7 @@ import './styles.css';
989989
import App from './App';
990990

991991
const root = createRoot(document.getElementById("root"));
992-
//Using StrictMode.
992+
//使用 StrictMode
993993
root.render(
994994
<StrictMode>
995995
<App />
@@ -1025,7 +1025,7 @@ export default function CatFriends() {
10251025
</nav>
10261026
<hr />
10271027
<nav>
1028-
<span>Scroll to:</span>{cats.map((cat, index) => (
1028+
<span>滚动到:</span>{cats.map((cat, index) => (
10291029
<button key={cat.src} onClick={() => scrollToCat(index)}>
10301030
{index}
10311031
</button>
@@ -1040,12 +1040,12 @@ export default function CatFriends() {
10401040
const list = itemsRef.current;
10411041
const item = {cat: cat, node};
10421042
list.push(item);
1043-
console.log(`Adding cat to the map. Total cats: ${list.length}`);
1043+
console.log(`将猫添加到 map 中。当前猫的总数:${list.length}`);
10441044
if (list.length > 10) {
1045-
console.log('Too many cats in the list!');
1045+
console.log('列表中的猫太多了!');
10461046
}
10471047
return () => {
1048-
// 🚩 No cleanup, this is a bug!
1048+
// 🚩 没有清理,这是一个错误!
10491049
}
10501050
}}
10511051
>
@@ -1100,9 +1100,9 @@ li {
11001100
11011101
</Sandpack>
11021102
1103-
**With Strict Mode, you immediately see that there is a problem**. Strict Mode runs an extra setup+cleanup cycle for every callback ref. This callback ref has no cleanup logic, so it adds refs but doesn't remove them. This is a hint that you're missing a cleanup function.
1103+
**在严格模式下,你立即就能看到存在问题**。严格模式为每个回调 ref 运行额外一次 setup+cleanup 循环。这个回调 ref 没有 cleanup 逻辑,所以它添加了 ref 但没有移除它们。这是一个提示,你可能忘记了添加清理函数。
11041104
1105-
Strict Mode lets you eagerly find mistakes in callback refs. When you fix your callback by adding a cleanup function in Strict Mode, you *also* fix many possible future production bugs like the "Scroll to" bug from before:
1105+
严格模式让你能够及早发现回调 ref 中的错误。当你在严格模式下通过添加清理函数来修复回调时,你也*同时*修复了许多可能在未来的生产环境中出现的错误,比如之前的「滚动到」错误:
11061106
11071107
<Sandpack>
11081108
@@ -1114,7 +1114,7 @@ import './styles.css';
11141114
import App from './App';
11151115

11161116
const root = createRoot(document.getElementById("root"));
1117-
//Using StrictMode.
1117+
//使用 StrictMode
11181118
root.render(
11191119
<StrictMode>
11201120
<App />
@@ -1150,7 +1150,7 @@ export default function CatFriends() {
11501150
</nav>
11511151
<hr />
11521152
<nav>
1153-
<span>Scroll to:</span>{cats.map((cat, index) => (
1153+
<span>滚动到:</span>{cats.map((cat, index) => (
11541154
<button key={cat.src} onClick={() => scrollToCat(index)}>
11551155
{index}
11561156
</button>
@@ -1165,13 +1165,13 @@ export default function CatFriends() {
11651165
const list = itemsRef.current;
11661166
const item = {cat: cat, node};
11671167
list.push(item);
1168-
console.log(`Adding cat to the map. Total cats: ${list.length}`);
1168+
console.log(`将猫添加到 map 中。当前猫的总数:${list.length}`);
11691169
if (list.length > 10) {
1170-
console.log('Too many cats in the list!');
1170+
console.log('列表中的猫太多了!');
11711171
}
11721172
return () => {
11731173
list.splice(list.indexOf(item), 1);
1174-
console.log(`Removing cat from the map. Total cats: ${itemsRef.current.length}`);
1174+
console.log(`将猫从 map 中移除。当前猫的总数:${itemsRef.current.length}`);
11751175
}
11761176
}}
11771177
>
@@ -1226,7 +1226,7 @@ li {
12261226
12271227
</Sandpack>
12281228
1229-
Now on inital mount in StrictMode, the ref callbacks are all setup, cleaned up, and setup again:
1229+
现在,在严格模式下的初始挂载中,ref 回调全部经历了 setup、cleanup、再次 setup 的过程:
12301230
12311231
```
12321232
...
@@ -1237,9 +1237,9 @@ Now on inital mount in StrictMode, the ref callbacks are all setup, cleaned up,
12371237
✅ Adding animal to the map. Total animals: 10
12381238
```
12391239
1240-
**This is expected.** Strict Mode confirms that the ref callbacks are cleaned up correctly, so the size never grows above the expected amount. After the fix, there are no memory leaks, and all the features work as expected.
1240+
**这是符合预期的。** 严格模式确认了 ref 回调被正确清理,因此数量永远不会超过预期值。修复之后,不存在内存泄漏,所有功能都按预期工作。
12411241
1242-
Without Strict Mode, it was easy to miss the bug until you clicked around to app to notice broken features. Strict Mode made the bugs appear right away, before you push them to production.
1242+
在没有严格模式的情况下,直到你在应用中点击并注意到功能被破坏之前,很容易忽视这个错误。严格模式让错误立即显现,避免在你将其推送到生产环境之前才被发现。
12431243
12441244
---
12451245
### 修复严格模式发出的弃用警告 {/*fixing-deprecation-warnings-enabled-by-strict-mode*/}

0 commit comments

Comments
 (0)