You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* `createHandle`: A function that takes no arguments and returns the ref handle you want to expose. That ref handle can have any type. Usually, you will return an object with the methods you want to expose.
* **optional** `dependencies`: The list of all reactive values referenced inside of the `createHandle`code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. If a re-render resulted in a change to some dependency, or if you omitted this argument, your `createHandle`function will re-execute, and the newly created handle will be assigned to the ref.
By default, components don't expose their DOM nodes to parent components. For example, if you want the parent component of `MyInput`to [have access](/learn/manipulating-the-dom-with-refs) to the `<input>` DOM node, you have to opt in with [`forwardRef`:](/reference/react/forwardRef)
57
+
默认情况下,组件不会将它们的 DOM 节点暴露给父组件。举例来说,如果你想要 `MyInput`的父组件 [能访问到](/learn/manipulating-the-dom-with-refs) `<input>` DOM 节点,你必须选择使用 [`forwardRef`:](/reference/react/forwardRef)。
With the code above, [a ref to `MyInput`will receive the`<input>` DOM node.](/reference/react/forwardRef#exposing-a-dom-node-to-the-parent-component) However, you can expose a custom value instead. To customize the exposed handle, call `useImperativeHandle` at the top level of your component:
67
+
在上方的代码中,[`MyInput`的 ref 会接收到`<input>` DOM 节点](/reference/react/forwardRef#exposing-a-dom-node-to-the-parent-component)。然而,你可以选择暴露一个自定义的值。为了修改被暴露的句柄,在你的顶层组件调用 `useImperativeHandle`:
Note that in the code above, the `ref`is no longer forwarded to the `<input>`.
83
+
注意在上述代码中,该 `ref`已不再被转发到 `<input>` 中。
84
84
85
-
For example, suppose you don't want to expose the entire `<input>` DOM node, but you want to expose two of its methods: `focus`and`scrollIntoView`. To do this, keep the real browser DOM in a separate ref. Then use `useImperativeHandle`to expose a handle with only the methods that you want the parent component to call:
85
+
举例来说,假设你不想暴露出整个 `<input>` DOM 节点,但你想要它其中两个方法:`focus`和`scrollIntoView`。为此,用单独额外的 ref 来指向真实的浏览器 DOM。然后使用 `useImperativeHandle`来暴露一个句柄,它只返回你想要父组件去调用的方法:
Now, if the parent component gets a ref to `MyInput`, it will be able to call the `focus`and`scrollIntoView`methods on it. However, it will not have full access to the underlying `<input>` DOM node.
108
+
现在,如果你的父组件获得了 `MyInput` 的 ref,就能通过该 ref 来调用 `focus`和`scrollIntoView`方法。然而,它的访问是受限的,无法读取或调用下方 `<input>` DOM 节点的其他所有属性和方法。
109
109
110
110
<Sandpack>
111
111
@@ -118,7 +118,7 @@ export default function Form() {
118
118
119
119
functionhandleClick() {
120
120
ref.current.focus();
121
-
//This won't work because the DOM node isn't exposed:
121
+
//下方代码不起作用,因为 DOM 节点并未被暴露出来:
122
122
// ref.current.style.opacity = 0.5;
123
123
}
124
124
@@ -166,9 +166,9 @@ input {
166
166
167
167
---
168
168
169
-
### Exposing your own imperative methods {/*exposing-your-own-imperative-methods*/}
The methods you expose via an imperative handle don't have to match the DOM methods exactly. For example, this `Post`component exposes a `scrollAndFocusAddComment`method via an imperative handle. This lets the parent `Page`scroll the list of comments *and* focus the input field when you click the button:
171
+
你通过命令式句柄暴露出来的方法不一定需要完全匹配 DOM 节点的方法。例如,这个 `Post`组件暴露了一个 `scrollAndFocusAddComment`方法。它可以让你在点击按钮后,使父组件 `Page`滚动到评论列表的底部 *并* 聚焦到输入框:
172
172
173
173
<Sandpack>
174
174
@@ -281,8 +281,8 @@ export default AddComment;
281
281
282
282
<Pitfall>
283
283
284
-
**Do not overuse refs.** You should only use refs for *imperative* behaviors that you can't express as props: for example, scrolling to a node, focusing a node, triggering an animation, selecting text, and so on.
**If you can express something as a prop, you should not use a ref.** For example, instead of exposing an imperative handle like `{ open, close}`from a `Modal` component, it is better to take `isOpen` as a prop like `<Modal isOpen={isOpen} />`. [Effects](/learn/synchronizing-with-effects) can help you expose imperative behaviors via props.
0 commit comments