Skip to content

docs(en): merge reactjs.org/main into zh-hans.reactjs.org/main @ 9fb2f0dd #1512

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
91 changes: 62 additions & 29 deletions src/content/learn/manipulating-the-dom-with-refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,19 @@ li {
<Sandpack>

```js
import { useRef } from 'react';
import { useRef, useState } from "react";

export default function CatFriends() {
const itemsRef = useRef(null);
const [catList, setCatList] = useState(setupCatList);

function scrollToId(itemId) {
function scrollToCat(cat) {
const map = getMap();
const node = map.get(itemId);
const node = map.get(cat);
node.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'center'
behavior: "smooth",
block: "nearest",
inline: "center",
});
}

Expand All @@ -246,34 +247,25 @@ export default function CatFriends() {
return (
<>
<nav>
<button onClick={() => scrollToId(0)}>
Tom
</button>
<button onClick={() => scrollToId(5)}>
Maru
</button>
<button onClick={() => scrollToId(9)}>
Jellylorum
</button>
<button onClick={() => scrollToCat(catList[0])}>Tom</button>
<button onClick={() => scrollToCat(catList[5])}>Maru</button>
<button onClick={() => scrollToCat(catList[9])}>Jellylorum</button>
</nav>
<div>
<ul>
{catList.map(cat => (
{catList.map((cat) => (
<li
key={cat.id}
key={cat}
ref={(node) => {
const map = getMap();
if (node) {
map.set(cat.id, node);
map.set(cat, node);
} else {
map.delete(cat.id);
map.delete(cat);
}
}}
>
<img
src={cat.imageUrl}
alt={'Cat #' + cat.id}
/>
<img src={cat} />
</li>
))}
</ul>
Expand All @@ -282,12 +274,13 @@ export default function CatFriends() {
);
}

const catList = [];
for (let i = 0; i < 10; i++) {
catList.push({
id: i,
imageUrl: 'https://placekitten.com/250/200?image=' + i
});
function setupCatList() {
const catList = [];
for (let i = 0; i < 10; i++) {
catList.push("https://loremflickr.com/320/240/cat?lock=" + i);
}

return catList;
}

```
Expand Down Expand Up @@ -318,6 +311,16 @@ li {
}
```

```json package.json hidden
{
"dependencies": {
"react": "canary",
"react-dom": "canary",
"react-scripts": "^5.0.0"
}
}
```

</Sandpack>

在这个例子中,`itemsRef` 保存的不是单个 DOM 节点,而是保存了包含列表项 ID 和 DOM 节点的 [Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)。([Ref 可以保存任何值!](/learn/referencing-values-with-refs)) 每个列表项上的 [`ref` 回调](/reference/react-dom/components/common#ref-callback)负责更新 Map:
Expand All @@ -328,18 +331,48 @@ li {
ref={node => {
const map = getMap();
if (node) {
<<<<<<< HEAD
// 添加到 Map
map.set(cat.id, node);
} else {
// 从 Map 删除
map.delete(cat.id);
=======
// Add to the Map
map.set(cat, node);
} else {
// Remove from the Map
map.delete(cat);
>>>>>>> 9fb2f0dd12ef0babc9dc30c0eb984e49a94a9a20
}
}}
>
```

这使你可以之后从 Map 读取单个 DOM 节点。

<Canary>

This example shows another approach for managing the Map with a `ref` callback cleanup function.

```js
<li
key={cat.id}
ref={node => {
const map = getMap();
// Add to the Map
map.set(cat, node);

return () => {
// Remove from the Map
map.delete(cat);
};
}}
>
```

</Canary>

</DeepDive>

## 访问另一个组件的 DOM 节点 {/*accessing-another-components-dom-nodes*/}
Expand Down
29 changes: 29 additions & 0 deletions src/content/reference/react-dom/components/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,40 @@ title: "普通组件(例如 <div>)"

#### 参数 {/*ref-callback-parameters*/}

<<<<<<< HEAD
* `node`:DOM 节点或 `null`。当回调函数被附加在 `ref` 属性后,触发回调时,该参数为对应的 DOM 节点。当 ref 被分离时值为 `null`。除非在每次渲染时都传递相同的函数引用作为 `ref` 回调,否则该回调将在组件的每次重新渲染期间被暂时分离和重新连接。
=======
* `node`: A DOM node or `null`. React will pass you the DOM node when the ref gets attached, and `null` when the `ref` gets detached. Unless you pass the same function reference for the `ref` callback on every render, the callback will get temporarily detached and re-attached during every re-render of the component.

<Canary>
>>>>>>> 9fb2f0dd12ef0babc9dc30c0eb984e49a94a9a20

#### 返回值 {/*returns*/}

<<<<<<< HEAD
不要从 `ref` 回调函数中返回任何内容。
=======
* **optional** `cleanup function`: When the `ref` is detached, React will call the cleanup function. If a function is not returned by the `ref` callback, React will call the callback again with `null` as the argument when the `ref` gets detached.

```js

<div ref={(node) => {
console.log(node);

return () => {
console.log('Clean up', node)
}
}}>

```

#### Caveats {/*caveats*/}

* When Strict Mode is on, React will **run one extra development-only setup+cleanup cycle** before the first real setup. This is a stress-test that ensures that your cleanup logic "mirrors" your setup logic and that it stops or undoes whatever the setup is doing. If this causes a problem, implement the cleanup function.
* When you pass a *different* `ref` callback, React will call the *previous* callback's cleanup function if provided. If not cleanup function is defined, the `ref` callback will be called with `null` as the argument. The *next* function will be called with the DOM node.

</Canary>
>>>>>>> 9fb2f0dd12ef0babc9dc30c0eb984e49a94a9a20

---

Expand Down
25 changes: 25 additions & 0 deletions src/content/reference/react/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,33 @@ translators:

---

<<<<<<< HEAD
* [`createContext`](/reference/react/createContext) API 可以创建一个 context,你可以将其提供给子组件,通常会与 [`useContext`](/reference/react/useContext) 一起配合使用。
* [`forwardRef`](/reference/react/forwardRef) 允许组件将 DOM 节点作为 ref 暴露给父组件。
* [`lazy`](/reference/react/lazy) 允许你延迟加载组件,直到该组件需要第一次被渲染。
* [`memo`](/reference/react/memo) 允许你在 props 没有变化的情况下跳过组件的重渲染。通常 [`useMemo`](/reference/react/useMemo) 与 [`useCallback`](/reference/react/useCallback) 会一起配合使用。
* [`startTransition`](/reference/react/startTransition) 允许你可以标记一个状态更新是不紧急的。类似于 [`useTransition`](/reference/react/useTransition)。
=======
* [`createContext`](/reference/react/createContext) lets you define and provide context to the child components. Used with [`useContext`.](/reference/react/useContext)
* [`forwardRef`](/reference/react/forwardRef) lets your component expose a DOM node as a ref to the parent. Used with [`useRef`.](/reference/react/useRef)
* [`lazy`](/reference/react/lazy) lets you defer loading a component's code until it's rendered for the first time.
* [`memo`](/reference/react/memo) lets your component skip re-renders with same props. Used with [`useMemo`](/reference/react/useMemo) and [`useCallback`.](/reference/react/useCallback)
* [`startTransition`](/reference/react/startTransition) lets you mark a state update as non-urgent. Similar to [`useTransition`.](/reference/react/useTransition)

---

## Resource APIs {/*resource-apis*/}

*Resources* can be accessed by a component without having them as part of their state. For example, a component can read a message from a Promise or read styling information from a context.

To read a value from a resource, use this API:

* [`use`](/reference/react/use) lets you read the value of a resource like a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
```js
function MessageComponent({ messagePromise }) {
const message = use(messagePromise);
const theme = use(ThemeContext);
// ...
}
```
>>>>>>> 9fb2f0dd12ef0babc9dc30c0eb984e49a94a9a20
4 changes: 4 additions & 0 deletions src/content/reference/react/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function TodoList({ todos, tab, theme }) {

---

<<<<<<< HEAD
## 资源 Hook {/*resource-hooks*/}

资源可以被组件访问,而无需将它们作为状态的一部分。例如,组件可以从 Promise 中读取消息,或从上下文中读取样式信息。
Expand All @@ -125,6 +126,9 @@ function MessageComponent({ messagePromise }) {
---

## 其他 Hook {/*other-hooks*/}
=======
## Other Hooks {/*other-hooks*/}
>>>>>>> 9fb2f0dd12ef0babc9dc30c0eb984e49a94a9a20

这些 Hook 主要适用于库作者,不常在应用程序代码中使用。

Expand Down
Loading
Loading