Skip to content

Commit b60e1f4

Browse files
committed
docs(cn): fix a typo and align with the spacing conventions
1 parent 69a6525 commit b60e1f4

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/content/learn/reusing-logic-with-custom-hooks.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ React 应用是由组件构成,而组件由内置或自定义 Hook 构成。
241241
如果你创建的函数没有调用任何 Hook 方法,在命名时应避免使用 `use` 前缀,把它当成一个常规函数去命名。如下案例中的 `useSorted` 函数就没有调用任何 Hook 方法,所以更推荐用 `getSorted` 来命名:
242242

243243
```js
244-
// 🔴 Avoid: 没有调用其他Hook的Hook
244+
// 🔴 避免:没有调用其他 Hook 的 Hook
245245
function useSorted(items) {
246246
return items.slice().sort();
247247
}
248248

249-
//Good: 没有使用Hook的常规函数
249+
//好:没有使用 Hook 的常规函数
250250
function getSorted(items) {
251251
return items.slice().sort();
252252
}
@@ -258,7 +258,7 @@ function getSorted(items) {
258258
function List({ items, shouldSort }) {
259259
let displayedItems = items;
260260
if (shouldSort) {
261-
//在条件分支里调用getSorted()是没问题的,因为它不是Hook
261+
//在条件分支里调用 getSorted() 是没问题的,因为它不是 Hook
262262
displayedItems = getSorted(items);
263263
}
264264
// ...
@@ -268,7 +268,7 @@ function List({ items, shouldSort }) {
268268
哪怕内部只使用了一个 Hook,你也应该给这个函数加 `use` 前缀(让它成为一个 Hook):
269269

270270
```js
271-
//Good: 一个使用了其他Hook的Hook
271+
//好:一个使用了其他 Hook 的 Hook
272272
function useAuth() {
273273
return useContext(Auth);
274274
}
@@ -277,7 +277,7 @@ function useAuth() {
277277
技术上 React 对此并不强制要求。原则上你可以写出不调用其他 Hook 的 Hook。但这常常会难以理解且受限,所以最好避免这种方式。但是它在极少数场景下可能是有益的。例如函数目前也许并没有使用任何 Hook,但是你计划未来在该函数内部添加一些 Hook 调用。那么使用 `use` 前缀命名就很有意义:
278278

279279
```js {3-4}
280-
//Good: 之后可能使用其他Hook的Hook
280+
//好:之后可能使用其他 Hook 的 Hook
281281
function useAuth() {
282282
// TODO: 当认证功能实现以后,替换这一行:
283283
// 返回 useContext(Auth);
@@ -449,7 +449,7 @@ function Form() {
449449
450450
每当组件重新渲染,自定义 Hook 中的代码就会重新运行。这就是组件和自定义 Hook 都 [需要是纯函数](/learn/keeping-components-pure) 的原因。我们应该把自定义 Hook 的代码看作组件主体的一部分。
451451
452-
由于自定义 Hook 会随着组件一起重新渲染,所以组件可以一直接收到最新的 props 和 state。想知道这意味着什么,那就看看这个聊天室的示例。修改 ServeUrl 或者 roomID
452+
由于自定义 Hook 会随着组件一起重新渲染,所以组件可以一直接收到最新的 props 和 state。想知道这意味着什么,那就看看这个聊天室的示例。修改 serverUrl 或者 roomId
453453
454454
<Sandpack>
455455
@@ -1207,7 +1207,7 @@ function ShippingForm({ country }) {
12071207
function ChatRoom({ roomId }) {
12081208
const [serverUrl, setServerUrl] = useState('https://localhost:1234');
12091209

1210-
// 🔴 Avoid: 使用自定义“生命周期” Hook
1210+
// 🔴 避免:使用自定义“生命周期” Hook
12111211
useMount(() => {
12121212
const connection = createConnection({ roomId, serverUrl });
12131213
connection.connect();
@@ -1217,7 +1217,7 @@ function ChatRoom({ roomId }) {
12171217
// ...
12181218
}
12191219

1220-
// 🔴 Avoid: 创建自定义“生命周期” Hook
1220+
// 🔴 避免:创建自定义“生命周期” Hook
12211221
function useMount(fn) {
12221222
useEffect(() => {
12231223
fn();
@@ -1233,7 +1233,7 @@ function useMount(fn) {
12331233
function ChatRoom({ roomId }) {
12341234
const [serverUrl, setServerUrl] = useState('https://localhost:1234');
12351235

1236-
//Good: 通过用途分割的两个原始Effect
1236+
//好:通过用途分割的两个原始Effect
12371237

12381238
useEffect(() => {
12391239
const connection = createConnection({ serverUrl, roomId });
@@ -1255,7 +1255,7 @@ function ChatRoom({ roomId }) {
12551255
function ChatRoom({ roomId }) {
12561256
const [serverUrl, setServerUrl] = useState('https://localhost:1234');
12571257

1258-
//Great: 以用途命名的自定义Hook
1258+
//非常好:以用途命名的自定义Hook
12591259
useChatRoom({ serverUrl, roomId });
12601260
useImpressionLog('visit_chat', { roomId });
12611261
// ...

0 commit comments

Comments
 (0)