diff --git a/content/docs/forwarding-refs.md b/content/docs/forwarding-refs.md index 3318d8499c..724a0bac47 100644 --- a/content/docs/forwarding-refs.md +++ b/content/docs/forwarding-refs.md @@ -1,76 +1,76 @@ --- id: forwarding-refs -title: Forwarding Refs +title: Refs 转发 permalink: docs/forwarding-refs.html --- -Ref forwarding is a technique for automatically passing a [ref](/docs/refs-and-the-dom.html) through a component to one of its children. This is typically not necessary for most components in the application. However, it can be useful for some kinds of components, especially in reusable component libraries. The most common scenarios are described below. +Ref 转发是一项将 [ref](/docs/refs-and-the-dom.html) 自动地通过组件传递到其一子组件的技巧。对于大多数应用中的组件来说,这通常不是必需的。但其对某些组件,尤其是可重用的组件库是很有用的。最常见的案例如下所述。 -## Forwarding refs to DOM components {#forwarding-refs-to-dom-components} +## 转发 refs 到 DOM 组件 {#forwarding-refs-to-dom-components} -Consider a `FancyButton` component that renders the native `button` DOM element: +考虑这个渲染原生 DOM 元素 `button` 的 `FancyButton` 组件: `embed:forwarding-refs/fancy-button-simple.js` -React components hide their implementation details, including their rendered output. Other components using `FancyButton` **usually will not need to** [obtain a ref](/docs/refs-and-the-dom.html) to the inner `button` DOM element. This is good because it prevents components from relying on each other's DOM structure too much. +React 组件隐藏其实现细节,包括其渲染结果。其他使用 `FancyButton` 的组件**通常不需要**获取内部的 DOM 元素 `button` 的 [ref](/docs/refs-and-the-dom.html)。这很好,因为这防止组件过度依赖其他组件的 DOM 结构。 -Although such encapsulation is desirable for application-level components like `FeedStory` or `Comment`, it can be inconvenient for highly reusable "leaf" components like `FancyButton` or `MyTextInput`. These components tend to be used throughout the application in a similar manner as a regular DOM `button` and `input`, and accessing their DOM nodes may be unavoidable for managing focus, selection, or animations. +虽然这种封装对类似 `FeedStory` 或 `Comment` 这样的应用级组件是理想的,但其对 `FancyButton` 或 `MyTextInput` 这样的高可复用“叶”组件来说可能是不方便的。这些组件倾向于在整个应用中以一种类似常规 DOM `button` 和 `input` 的方式被使用,并且访问其 DOM 节点对管理焦点,选中或动画来说是不可避免的。 -**Ref forwarding is an opt-in feature that lets some components take a `ref` they receive, and pass it further down (in other words, "forward" it) to a child.** +**Ref 转发是一个可选特性,其允许某些组件接收 `ref`,并将其向下传递(换句话说,“转发”它)给子组件。** -In the example below, `FancyButton` uses `React.forwardRef` to obtain the `ref` passed to it, and then forward it to the DOM `button` that it renders: +在下面的示例中,`FancyButton` 使用 `React.forwardRef` 来获取传递给它的 `ref`,然后转发到它渲染的 DOM `button`: `embed:forwarding-refs/fancy-button-simple-ref.js` -This way, components using `FancyButton` can get a ref to the underlying `button` DOM node and access it if necessary—just like if they used a DOM `button` directly. +这样,使用 `FancyButton` 的组件可以获取底层 DOM 节点 `button` 的 ref ,并在必要时访问,就像其直接使用 DOM `button` 一样。 -Here is a step-by-step explanation of what happens in the above example: +以下是对上述示例发生情况的逐步解释: -1. We create a [React ref](/docs/refs-and-the-dom.html) by calling `React.createRef` and assign it to a `ref` variable. -1. We pass our `ref` down to `` by specifying it as a JSX attribute. -1. React passes the `ref` to the `(props, ref) => ...` function inside `forwardRef` as a second argument. -1. We forward this `ref` argument down to ` )); -// You can now get a ref directly to the DOM button: +// 你可以直接获取 DOM button 的 ref: const ref = React.createRef(); Click me!; diff --git a/examples/forwarding-refs/fancy-button.js b/examples/forwarding-refs/fancy-button.js index 9dcd13e165..0fc76848e8 100644 --- a/examples/forwarding-refs/fancy-button.js +++ b/examples/forwarding-refs/fancy-button.js @@ -6,7 +6,7 @@ class FancyButton extends React.Component { // ... } -// Rather than exporting FancyButton, we export LogProps. -// It will render a FancyButton though. +// 我们导出 LogProps,而不是 FancyButton。 +// 虽然它也会渲染一个 FancyButton。 // highlight-next-line export default logProps(FancyButton); diff --git a/examples/forwarding-refs/log-props-after.js b/examples/forwarding-refs/log-props-after.js index a603bd697d..87fed101c8 100644 --- a/examples/forwarding-refs/log-props-after.js +++ b/examples/forwarding-refs/log-props-after.js @@ -9,15 +9,15 @@ function logProps(Component) { // highlight-next-line const {forwardedRef, ...rest} = this.props; - // Assign the custom prop "forwardedRef" as a ref + // 将自定义的 prop 属性 “forwardedRef” 定义为 ref // highlight-next-line return ; } } - // Note the second param "ref" provided by React.forwardRef. - // We can pass it along to LogProps as a regular prop, e.g. "forwardedRef" - // And it can then be attached to the Component. + // 注意 React.forwardRef 回调的第二个参数 “ref”。 + // 我们可以将其作为常规 prop 属性传递给 LogProps,例如 “forwardedRef” + // 然后它就可以被挂载到被 LogPros 包裹的子组件上。 // highlight-range{1-3} return React.forwardRef((props, ref) => { return ;