Skip to content

Commit dc1c4af

Browse files
committed
translate: update reference/react/index.md
1 parent 82e5b3b commit dc1c4af

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

src/content/reference/react/index.md

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
---
2-
title: "Built-in React Hooks"
2+
title: "内置 React Hooks"
33
---
44

55
<Intro>
66

7-
*Hooks* let you use different React features from your components. You can either use the built-in Hooks or combine them to build your own. This page lists all built-in Hooks in React.
7+
*Hooks* 让你从你的组件中使用不同的 React 功能。你可以使用内置的 Hooks 或者结合它们来建立你自己的。本页列出了 React 中所有的内置 Hooks
88

99
</Intro>
1010

1111
---
1212

1313
## State Hooks {/*state-hooks*/}
1414

15-
*State* lets a component ["remember" information like user input.](/learn/state-a-components-memory) For example, a form component can use state to store the input value, while an image gallery component can use state to store the selected image index.
15+
*State* 让一个组件 ["记住" 像用户输入的信息](/learn/state-a-components-memory),比如,要给一个组件添加状态,使用这些 Hooks 中的一个:
1616

17-
To add state to a component, use one of these Hooks:
18-
19-
* [`useState`](/reference/react/useState) declares a state variable that you can update directly.
20-
* [`useReducer`](/reference/react/useReducer) declares a state variable with the update logic inside a [reducer function.](/learn/extracting-state-logic-into-a-reducer)
17+
* [`useState`](/reference/react/useState) 声明了一个你可以直接更新的状态变量。
18+
* [`useReducer`](/reference/react/useReducer) 声明了一个带有更新逻辑的状态变量在一个 [reducer 函数](/learn/extracting-state-logic-into-a-reducer)中。
2119

2220
```js
2321
function ImageGallery() {
@@ -29,9 +27,9 @@ function ImageGallery() {
2927
3028
## Context Hooks {/*context-hooks*/}
3129
32-
*Context* lets a component [receive information from distant parents without passing it as props.](/learn/passing-props-to-a-component) For example, your app's top-level component can pass the current UI theme to all components below, no matter how deep.
30+
*Context* 让一个组件[从远处的父组件接收信息,而不需要将其作为 props 传递。](/learn/passing-props-to-a-component) 比如,app 的顶层组件可以将当前的 UI 主题传递给下面的所有组件,无论它们层级多深。
3331
34-
* [`useContext`](/reference/react/useContext) reads and subscribes to a context.
32+
* [`useContext`](/reference/react/useContext) 读取并订阅了一个上下文。
3533
3634
```js
3735
function Button() {
@@ -43,10 +41,10 @@ function Button() {
4341
4442
## Ref Hooks {/*ref-hooks*/}
4543
46-
*Refs* let a component [hold some information that isn't used for rendering,](/learn/referencing-values-with-refs) like a DOM node or a timeout ID. Unlike with state, updating a ref does not re-render your component. Refs are an "escape hatch" from the React paradigm. They are useful when you need to work with non-React systems, such as the built-in browser APIs.
44+
*Ref* 让一个组件[持有一些不用于渲染的信息,](/learn/referencing-values-with-refs)DOM 节点或一个 timeout ID。与 state 不同的是,更新 Ref 并不会重新渲染你的组件。Ref 是 React 范式的一个 "逃生舱门"。当你需要与非 React 系统一起工作时,它们很有用,比如内置的浏览器 API。
4745
48-
* [`useRef`](/reference/react/useRef) declares a ref. You can hold any value in it, but most often it's used to hold a DOM node.
49-
* [`useImperativeHandle`](/reference/react/useImperativeHandle) lets you customize the ref exposed by your component. This is rarely used.
46+
* [`useRef`](/reference/react/useRef) 声明了一个 Ref。你可以在其中保存任何值,但最常见的是它用来保存一个 DOM 节点。
47+
* [`useImperativeHandle`](/reference/react/useImperativeHandle) 可以让你自定义你的组件所暴露的 Ref。这一点很少使用。
5048
5149
```js
5250
function Form() {
@@ -58,9 +56,9 @@ function Form() {
5856
5957
## Effect Hooks {/*effect-hooks*/}
6058
61-
*Effects* let a component [connect to and synchronize with external systems.](/learn/synchronizing-with-effects) This includes dealing with network, browser DOM, animations, widgets written using a different UI library, and other non-React code.
59+
*Effect* 让一个组件[连接到外部系统并与之同步。](/learn/synchronizing-with-effects)这包括处理网络、浏览器、DOM、动画、使用不同 UI 库编写的 widgets 以及其他非 React 代码。
6260
63-
* [`useEffect`](/reference/react/useEffect) connects a component to an external system.
61+
* [`useEffect`](/reference/react/useEffect) 将一个组件连接到外部系统。
6462
6563
```js
6664
function ChatRoom({ roomId }) {
@@ -72,23 +70,23 @@ function ChatRoom({ roomId }) {
7270
// ...
7371
```
7472
75-
Effects are an "escape hatch" from the React paradigm. Don't use Effects to orchestrate the data flow of your application. If you're not interacting with an external system, [you might not need an Effect.](/learn/you-might-not-need-an-effect)
73+
Effect 是 React 范式的 "逃生舱门"。不要用 Effect 来协调你的应用程序的数据流。如果你不与外部系统交互,[你可能不需要 Effect](/learn/you-might-not-need-an-effect)
7674
77-
There are two rarely used variations of `useEffect` with differences in timing:
75+
`useEffect` 有两个很少使用的变体,它们在时间上有差异:
7876
79-
* [`useLayoutEffect`](/reference/react/useLayoutEffect) fires before the browser repaints the screen. You can measure layout here.
80-
* [`useInsertionEffect`](/reference/react/useInsertionEffect) fires before React makes changes to the DOM. Libraries can insert dynamic CSS here.
77+
* [`useLayoutEffect`](/reference/react/useLayoutEffect) 在浏览器重绘屏幕前启动。你可以在这里测量布局。
78+
* [`useInsertionEffect`](/reference/react/useInsertionEffect) React DOM 进行修改之前触发。Libraries 可以在这里插入动态 CSS
8179
8280
---
8381
8482
## Performance Hooks {/*performance-hooks*/}
8583
86-
A common way to optimize re-rendering performance is to skip unnecessary work. For example, you can tell React to reuse a cached calculation or to skip a re-render if the data has not changed since the previous render.
84+
优化重新渲染性能的一个常见方法就是跳过不必要的工作。比如,你可以告诉 React 重用一个缓存的计算,或者如果数据在上一次渲染后没有变化,就跳过一次重新渲染。
8785
88-
To skip calculations and unnecessary re-rendering, use one of these Hooks:
86+
要跳过计算和不必要的重新渲染,请使用这些 Hooks 的一个:
8987
90-
- [`useMemo`](/reference/react/useMemo) lets you cache the result of an expensive calculation.
91-
- [`useCallback`](/reference/react/useCallback) lets you cache a function definition before passing it down to an optimized component.
88+
* [`useMemo`](/reference/react/useMemo) 让你缓存一个高昂的计算结果。
89+
* [`useCallback`](/reference/react/useCallback) 让你在将一个函数定义传递给一个优化的组件之前缓存它。
9290
9391
```js
9492
function TodoList({ todos, tab, theme }) {
@@ -97,25 +95,25 @@ function TodoList({ todos, tab, theme }) {
9795
}
9896
```
9997
100-
Sometimes, you can't skip re-rendering because the screen actually needs to update. In that case, you can improve performance by separating blocking updates that must be synchronous (like typing into an input) from non-blocking updates which don't need to block the user interface (like updating a chart).
98+
有时,你不能跳过重新渲染,因为屏幕确实需要更新。在这种情况下,你可以通过将必须同步的阻塞更新(比如向输入法输入)与不需要阻塞用户界面的非阻塞更新(比如更新图表)分开以提高性能。
10199
102-
To prioritize rendering, use one of these Hooks:
100+
要确定渲染的优先级,可以使用这些 Hooks 的一个:
103101
104-
- [`useTransition`](/reference/react/useTransition) lets you mark a state transition as non-blocking and allow other updates to interrupt it.
105-
- [`useDeferredValue`](/reference/react/useDeferredValue) lets you defer updating a non-critical part of the UI and let other parts update first.
102+
* [`useTransition`](/reference/react/useTransition) 让你把一个状态转换标记为非阻塞,并允许其他更新中断它。
103+
* [`useDeferredValue`](/reference/react/useDeferredValue) 让你推迟更新用户界面的一个非关键部分,让其他部分更新。
106104
107105
---
108106
109107
## Other Hooks {/*other-hooks*/}
110108
111-
These Hooks are mostly useful to library authors and aren't commonly used in the application code.
109+
这些 Hooks 主要对 library 的作者有用,在应用代码中并不常用。
112110
113-
- [`useDebugValue`](/reference/react/useDebugValue) lets you customize the label React DevTools displays for your custom Hook.
114-
- [`useId`](/reference/react/useId) lets a component associate a unique ID with itself. Typically used with accessibility APIs.
115-
- [`useSyncExternalStore`](/reference/react/useSyncExternalStore) lets a component subscribe to an external store.
111+
* [`useDebugValue`](/reference/react/useDebugValue) 让你在 React DevTools 中为自定义 Hook 添加一个 label。
112+
* [`useId`](/reference/react/useId) 让组件将一个独特的 ID 与自己联系起来。通常与可访问性 API 一起使用。
113+
* [`useSyncExternalStore`](/reference/react/useSyncExternalStore) 让一个组件订阅一个外部 store
116114
117115
---
118116
119117
## Your own Hooks {/*your-own-hooks*/}
120118
121-
You can also [define your own custom Hooks](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component) as JavaScript functions.
119+
你也可以在 JavaScript 函数中[定义你自己的 Hooks](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component)

0 commit comments

Comments
 (0)