Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(cn): translate reference/react/index into Chinese #1098

Merged
merged 53 commits into from
Jun 21, 2023
Merged
Changes from 28 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
74e72da
translate: update reference/react/index.md
thehelium Mar 24, 2023
c540245
translate: some words updated to singular
thehelium Mar 24, 2023
f4c22c8
translate: some words updated to singular
thehelium Mar 24, 2023
af71afe
translate: update commit suggestion
thehelium Mar 26, 2023
4d3e177
translate: update omissions
thehelium Mar 26, 2023
0543ecb
translate: update the word 'state'
thehelium Mar 28, 2023
5c3357c
fix(words): change some words
thehelium Mar 28, 2023
0146f3f
fix: update doc layout
thehelium Mar 30, 2023
a75761e
fix: update doc layout
thehelium Mar 30, 2023
e97c2ee
fix: update the sentence
thehelium Mar 30, 2023
3dcf7d4
fix: update src/content/reference/react/index.md
thehelium Apr 9, 2023
42f35fb
fix: update src/content/reference/react/index.md
thehelium Apr 9, 2023
650d0be
fix: update src/content/reference/react/index.md
thehelium Apr 9, 2023
49b6aa5
fix: update src/content/reference/react/index.md
thehelium Apr 11, 2023
f1cd8ce
fix: update src/content/reference/react/index.md
thehelium Apr 11, 2023
edc05ef
fix: update src/content/reference/react/index.md
thehelium Apr 11, 2023
76082d0
fix: update src/content/reference/react/index.md
thehelium Apr 11, 2023
75eb1d0
fix: update src/content/reference/react/index.md
thehelium Apr 12, 2023
d615019
fix: update src/content/reference/react/index.md
thehelium Apr 12, 2023
adc4ac1
fix: update src/content/reference/react/index.md
thehelium Apr 12, 2023
fe47727
fix: update src/content/reference/react/index.md
thehelium Apr 12, 2023
4a269e3
fix: update some strange words
thehelium Apr 12, 2023
8fe9943
fix: update src/content/reference/react/index.md
thehelium Apr 20, 2023
b114d3a
fix: update src/content/reference/react/index.md
thehelium Apr 20, 2023
7ce4bf5
fix: update src/content/reference/react/index.md
thehelium Apr 20, 2023
a8ad575
fix: update src/content/reference/react/index.md
thehelium Apr 20, 2023
79dfd73
Merge branch 'main' into translate-reference-index
thehelium Apr 25, 2023
87cea45
Merge branch 'main' into translate-reference-index
awxiaoxian2020 Apr 27, 2023
90cf587
Merge branch 'reactjs:main' into translate-reference-index
thehelium May 9, 2023
3894dc9
fix: update the term "escape hatches"
thehelium May 9, 2023
621868c
fix: update the term "escape hatch"
thehelium May 9, 2023
7b264c0
Merge branch 'main' into translate-reference-index
thehelium May 10, 2023
d2057be
Merge branch 'main' into translate-reference-index
thehelium May 12, 2023
d79a5c2
fix: update reviewed
thehelium May 19, 2023
ee19c8f
fix: update reviewed
thehelium May 19, 2023
8642915
fix: update reviewed
thehelium May 19, 2023
9c9bb83
fix: update reviewed
thehelium May 19, 2023
94262b3
fix: update reviewed
thehelium May 19, 2023
a8ee6b9
fix: update reviewed
thehelium May 19, 2023
a74e906
fix: update reviewed
thehelium May 19, 2023
9090cc4
fix: update reviewed
thehelium May 19, 2023
4b38404
fix: update reviewed
thehelium May 19, 2023
4b60acc
fix: update reviewed
thehelium May 19, 2023
7699738
fix: update reviewed
thehelium May 19, 2023
e33f7ec
fix: update reviewed
thehelium May 19, 2023
89df8d3
fix: update reviewed
thehelium May 19, 2023
0a09a15
fix: update reviewed
thehelium May 19, 2023
7a2e4d8
fix: update reviewed
thehelium May 19, 2023
f9a1d90
Merge branch 'main' into translate-reference-index
thehelium May 19, 2023
184000a
Update src/content/reference/react/index.md
Yucohny Jun 19, 2023
df0681d
Update src/content/reference/react/index.md
Yucohny Jun 19, 2023
a8c5d6a
Update src/content/reference/react/index.md
Yucohny Jun 19, 2023
cd04727
Update src/content/reference/react/index.md
Yucohny Jun 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions src/content/reference/react/index.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
---
title: "Built-in React Hooks"
title: "内置 React Hook"
awxiaoxian2020 marked this conversation as resolved.
Show resolved Hide resolved
---

<Intro>

*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.
**Hook** 让你可以在组件中使用不同的 React 功能。你可以使用内置的 Hook 或者把它们组合起来建立你自己的。本页列出了 React 中所有的内置 Hook。

</Intro>

---

## State Hooks {/*state-hooks*/}
## 状态 Hook {/*state-hooks*/}

*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.
**state** 让一个组件 [“记住”像用户输入的信息](/learn/state-a-components-memory),比如,一个表单组件可以使用 state 来存储输入值,而一个图片库组件可以使用 state 来存储选定的图片索引。
thehelium marked this conversation as resolved.
Show resolved Hide resolved

To add state to a component, use one of these Hooks:
要给一个组件添加状态,可以使用这些 Hook 中的一个:
thehelium marked this conversation as resolved.
Show resolved Hide resolved

* [`useState`](/reference/react/useState) declares a state variable that you can update directly.
* [`useReducer`](/reference/react/useReducer) declares a state variable with the update logic inside a [reducer function.](/learn/extracting-state-logic-into-a-reducer)
* [`useState`](/reference/react/useState) 声明了一个你可以直接更新的 state 变量。
* [`useReducer`](/reference/react/useReducer) 声明了一个带有更新逻辑的 state 变量在一个 [reducer 函数](/learn/extracting-state-logic-into-a-reducer) 中。

```js
function ImageGallery() {
Expand All @@ -27,11 +27,11 @@ function ImageGallery() {

---

## Context Hooks {/*context-hooks*/}
## Context Hook {/*context-hooks*/}

*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.
*context* 让一个组件 [从远处的父组件接收信息,而不需要将其作为 props 传递](/learn/passing-props-to-a-component)。比如,app 的顶层组件可以将当前的 UI 主题传递给下面的所有组件,无论它们层级多深。

* [`useContext`](/reference/react/useContext) reads and subscribes to a context.
* [`useContext`](/reference/react/useContext) 读取并订阅了一个 context
thehelium marked this conversation as resolved.
Show resolved Hide resolved

```js
function Button() {
Expand All @@ -41,12 +41,12 @@ function Button() {

---

## Ref Hooks {/*ref-hooks*/}
## Ref Hook {/*ref-hooks*/}

*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.
*ref* 让一个组件 [持有一些不用于渲染的信息](/learn/referencing-values-with-refs),如 DOM 节点或一个 timeout ID。与 state 不同的是,更新 ref 并不会重新渲染你的组件。ref 是 React 范式的一个 "逃生舱门"。当你需要与非 React 系统一起工作时,它们很有用,比如内置的浏览器 API。

* [`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.
* [`useImperativeHandle`](/reference/react/useImperativeHandle) lets you customize the ref exposed by your component. This is rarely used.
* [`useRef`](/reference/react/useRef) 声明了一个 ref。你可以在其中保存任何值,但最常见的是它用来保存一个 DOM 节点。
thehelium marked this conversation as resolved.
Show resolved Hide resolved
* [`useImperativeHandle`](/reference/react/useImperativeHandle) 可以让你自定义你的组件所暴露的 ref。这一点很少使用。
thehelium marked this conversation as resolved.
Show resolved Hide resolved

```js
function Form() {
Expand All @@ -56,11 +56,11 @@ function Form() {

---

## Effect Hooks {/*effect-hooks*/}
## Effect Hook {/*effect-hooks*/}

*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.
*Effect* 让一个组件 [连接到外部系统并与之同步](/learn/synchronizing-with-effects)。这包括处理网络、浏览器、DOM、动画、使用不同 UI 库编写的 widgets 以及其他非 React 代码。

* [`useEffect`](/reference/react/useEffect) connects a component to an external system.
* [`useEffect`](/reference/react/useEffect) 将一个组件连接到外部系统。

```js
function ChatRoom({ roomId }) {
Expand All @@ -72,23 +72,23 @@ function ChatRoom({ roomId }) {
// ...
```

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)
Effect 是 React 范式的“逃生舱门”。不要用 Effect 来协调你的应用程序的数据流。如果你不与外部系统交互,[你可能不需要 Effect](/learn/you-might-not-need-an-effect)

There are two rarely used variations of `useEffect` with differences in timing:
`useEffect` 有两个很少使用的变换形式,它们在时间上有差异:
thehelium marked this conversation as resolved.
Show resolved Hide resolved

* [`useLayoutEffect`](/reference/react/useLayoutEffect) fires before the browser repaints the screen. You can measure layout here.
* [`useInsertionEffect`](/reference/react/useInsertionEffect) fires before React makes changes to the DOM. Libraries can insert dynamic CSS here.
* [`useLayoutEffect`](/reference/react/useLayoutEffect) 在浏览器重新绘制屏幕前启动。在这里,你可以测量布局。
thehelium marked this conversation as resolved.
Show resolved Hide resolved
* [`useInsertionEffect`](/reference/react/useInsertionEffect) React DOM 进行更改之前触发。在这里,库可以插入动态的 CSS

---

## Performance Hooks {/*performance-hooks*/}
## 性能 Hook {/*performance-hooks*/}

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.
优化重新渲染性能的一个常见方法就是跳过不必要的工作。比如,你可以告诉 React 重用一个缓存的计算,或者如果数据在上一次渲染后没有变化,就跳过一次重新渲染。
thehelium marked this conversation as resolved.
Show resolved Hide resolved

To skip calculations and unnecessary re-rendering, use one of these Hooks:
要跳过计算和不必要的重新渲染,请使用这些 Hook 的一个:
thehelium marked this conversation as resolved.
Show resolved Hide resolved

- [`useMemo`](/reference/react/useMemo) lets you cache the result of an expensive calculation.
- [`useCallback`](/reference/react/useCallback) lets you cache a function definition before passing it down to an optimized component.
* [`useMemo`](/reference/react/useMemo) 让你缓存一个非常高的计算结果。
thehelium marked this conversation as resolved.
Show resolved Hide resolved
* [`useCallback`](/reference/react/useCallback) 让你在将一个函数定义传递给一个优化的组件之前缓存它。

```js
function TodoList({ todos, tab, theme }) {
Expand All @@ -97,25 +97,25 @@ function TodoList({ todos, tab, theme }) {
}
```

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).
有时,你不能跳过重新渲染,因为屏幕确实需要更新。在这种情况下,你可以通过将必须同步的阻塞更新(比如向输入法输入)与不需要阻塞用户界面的非阻塞更新(比如更新图表)分开以提高性能。
thehelium marked this conversation as resolved.
Show resolved Hide resolved

To prioritize rendering, use one of these Hooks:
要确定渲染的优先级,可以从以下 Hook 选择其中之一:
thehelium marked this conversation as resolved.
Show resolved Hide resolved

- [`useTransition`](/reference/react/useTransition) lets you mark a state transition as non-blocking and allow other updates to interrupt it.
- [`useDeferredValue`](/reference/react/useDeferredValue) lets you defer updating a non-critical part of the UI and let other parts update first.
* [`useTransition`](/reference/react/useTransition) 让你把一个状态转换标记为非阻塞,并允许其他更新中断它。
* [`useDeferredValue`](/reference/react/useDeferredValue) 让你推迟更新用户界面的一个非关键部分,让其他部分更新。

---

## Other Hooks {/*other-hooks*/}
## 其他 Hook {/*other-hooks*/}

These Hooks are mostly useful to library authors and aren't commonly used in the application code.
这些 Hook 主要对库的作者有用,在应用代码中并不常用。

- [`useDebugValue`](/reference/react/useDebugValue) lets you customize the label React DevTools displays for your custom Hook.
- [`useId`](/reference/react/useId) lets a component associate a unique ID with itself. Typically used with accessibility APIs.
- [`useSyncExternalStore`](/reference/react/useSyncExternalStore) lets a component subscribe to an external store.
* [`useDebugValue`](/reference/react/useDebugValue) 让你在 React 开发者工具中为自定义 Hook 添加一个 label。
thehelium marked this conversation as resolved.
Show resolved Hide resolved
* [`useId`](/reference/react/useId) 让组件将一个独特的 ID 与自己联系起来。通常与可访问性 API 一起使用。
thehelium marked this conversation as resolved.
Show resolved Hide resolved
* [`useSyncExternalStore`](/reference/react/useSyncExternalStore) 让一个组件订阅一个外部 store
Yucohny marked this conversation as resolved.
Show resolved Hide resolved

---

## Your own Hooks {/*your-own-hooks*/}
## 自己的 Hook {/*your-own-hooks*/}
thehelium marked this conversation as resolved.
Show resolved Hide resolved

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.
你也可以在 JavaScript 函数中 [定义你自己的 Hook](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component)