diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 9b56fcc9f2..7c8f981779 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -11,33 +11,33 @@ redirect_from: permalink: docs/refs-and-the-dom.html --- -Refs provide a way to access DOM nodes or React elements created in the render method. +Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法中创建的 React 元素。 -In the typical React dataflow, [props](/docs/components-and-props.html) are the only way that parent components interact with their children. To modify a child, you re-render it with new props. However, there are a few cases where you need to imperatively modify a child outside of the typical dataflow. The child to be modified could be an instance of a React component, or it could be a DOM element. For both of these cases, React provides an escape hatch. +在典型的 React 数据流中,[props](/docs/components-and-props.html) 是父组件与子组件交互的唯一方式。要修改一个子组件,你需要使用新的 props 来重新渲染它。但是,在某些情况下,你需要在典型数据流之外强制修改子组件。被修改的子组件可能是一个 React 组件的实例,也可能是一个 DOM 元素。对于这两种情况,React 都提供了解决办法。 -### When to Use Refs {#when-to-use-refs} +### 何时使用 Refs {#when-to-use-refs} -There are a few good use cases for refs: +下面是几个适合使用 refs 的情况: -* Managing focus, text selection, or media playback. -* Triggering imperative animations. -* Integrating with third-party DOM libraries. +* 管理焦点,文本选择或媒体播放。 +* 触发强制动画。 +* 集成第三方 DOM 库。 -Avoid using refs for anything that can be done declaratively. +避免使用 refs 来做任何可以通过声明式实现来完成的事情。 -For example, instead of exposing `open()` and `close()` methods on a `Dialog` component, pass an `isOpen` prop to it. +举个例子,避免在 `Dialog` 组件里暴露 `open()` 和 `close()` 方法,最好传递 `isOpen` 属性。 -### Don't Overuse Refs {#dont-overuse-refs} +### 勿过度使用 Refs {#dont-overuse-refs} -Your first inclination may be to use refs to "make things happen" in your app. If this is the case, take a moment and think more critically about where state should be owned in the component hierarchy. Often, it becomes clear that the proper place to "own" that state is at a higher level in the hierarchy. See the [Lifting State Up](/docs/lifting-state-up.html) guide for examples of this. +你可能首先会想到使用 refs 在你的 app 中“让事情发生”。如果是这种情况,请花一点时间,认真再考虑一下 state 属性应该被安排在哪个组件层中。通常你会想明白,让更高的组件层级拥有这个 state,是更恰当的。查看 [状态提升](/docs/lifting-state-up.html) 以获取更多有关示例。 -> Note +> 注意 > -> The examples below have been updated to use the `React.createRef()` API introduced in React 16.3. If you are using an earlier release of React, we recommend using [callback refs](#callback-refs) instead. +> 下面的例子已经更新为使用在 React 16.3 版本引入的 `React.createRef()` API。如果你正在使用一个较早版本的 React,我们推荐你使用[回调形式的 refs](#callback-refs)。 -### Creating Refs {#creating-refs} +### 创建 Refs {#creating-refs} -Refs are created using `React.createRef()` and attached to React elements via the `ref` attribute. Refs are commonly assigned to an instance property when a component is constructed so they can be referenced throughout the component. +Refs 是使用 `React.createRef()` 创建的,并通过 `ref` 属性附加到 React 元素。在构造组件时,通常将 Refs 分配给实例属性,以便可以在整个组件中引用它们。 ```javascript{4,7} class MyComponent extends React.Component { @@ -51,44 +51,44 @@ class MyComponent extends React.Component { } ``` -### Accessing Refs {#accessing-refs} +### 访问 Refs {#accessing-refs} -When a ref is passed to an element in `render`, a reference to the node becomes accessible at the `current` attribute of the ref. +当 ref 被传递给 `render` 中的元素时,对该节点的引用可以在 ref 的 `current` 属性中被访问。 ```javascript const node = this.myRef.current; ``` -The value of the ref differs depending on the type of the node: +ref 的值根据节点的类型而有所不同: -- When the `ref` attribute is used on an HTML element, the `ref` created in the constructor with `React.createRef()` receives the underlying DOM element as its `current` property. -- When the `ref` attribute is used on a custom class component, the `ref` object receives the mounted instance of the component as its `current`. -- **You may not use the `ref` attribute on function components** because they don't have instances. +- 当 `ref` 属性用于 HTML 元素时,构造函数中使用 `React.createRef()` 创建的 `ref` 接收底层 DOM 元素作为其 `current` 属性。 +- 当 `ref` 属性用于自定义 class 组件时,`ref` 对象接收组件的挂载实例作为其 `current` 属性。 +- **你不能在函数组件上使用 `ref` 属性**,因为他们没有实例。 -The examples below demonstrate the differences. +以下例子说明了这些差异。 -#### Adding a Ref to a DOM Element {#adding-a-ref-to-a-dom-element} +#### 为 DOM 元素添加 ref {#adding-a-ref-to-a-dom-element} -This code uses a `ref` to store a reference to a DOM node: +以下代码使用 `ref` 去存储 DOM 节点的引用: ```javascript{5,12,22} class CustomTextInput extends React.Component { constructor(props) { super(props); - // create a ref to store the textInput DOM element + // 创建一个 ref 来存储 textInput 的 DOM 元素 this.textInput = React.createRef(); this.focusTextInput = this.focusTextInput.bind(this); } focusTextInput() { - // Explicitly focus the text input using the raw DOM API - // Note: we're accessing "current" to get the DOM node + // 直接使用原生 API 使 text 输入框获得焦点 + // 注意:我们通过 "current" 来访问 DOM 节点 this.textInput.current.focus(); } render() { - // tell React that we want to associate the ref - // with the `textInput` that we created in the constructor + // 告诉 React 我们想把 ref 关联到 + // 构造器里创建的 `textInput` 上 return (