From 52979c46672e3c4393dda9dab9bee148a32cd409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=90=E6=A0=A1=E9=95=B7?= Date: Sat, 2 Mar 2019 12:43:31 +0800 Subject: [PATCH 01/17] docs(cn): translate content/docs/refs-and-the-dom.md into Chinese --- content/docs/refs-and-the-dom.md | 123 ++++++++++++++++--------------- 1 file changed, 62 insertions(+), 61 deletions(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 9b56fcc9f2..8d4a34c5c5 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -11,33 +11,34 @@ 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,是更恰当的。查看 [Lifting State Up](/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。如果你正在使用一个较早的 Release 版本的 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 +52,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 (
{ - // Focus the text input using the raw DOM API + // 使用原生 DOM API 使 text 输入框获得焦点 if (this.textInput) this.textInput.focus(); }; } componentDidMount() { - // autofocus the input on mount + // 组件挂载后,让文本框自动获得焦点 this.focusTextInput(); } render() { - // Use the `ref` callback to store a reference to the text input DOM - // element in an instance field (for example, this.textInput). + // 使用 `ref` 的回调函数将 text 输入框 DOM 节点的引用存储到 React + // 实例上(比如 this.textInput) return (
`. As a result, `this.inputElement` in `Parent` will be set to the DOM node corresponding to the `` element in the `CustomTextInput`. +在上面的例子中,`Parent` 把它的 refs 回调函数当作 `inputRef` props 传递给了 `CustomTextInput`,而且 `CustomTextInput` 把相同的函数作为特殊的 `ref` 属性传递给了 ``。结果是,在 `Parent` 中的 `this.inputElement` 会被设置为与 `CustomTextInput` 中的 `input` 元素相对应的 DOM 节点。 -### Legacy API: String Refs {#legacy-api-string-refs} +### 旧版 API:String 类型的 Refs {#legacy-api-string-refs} -If you worked with React before, you might be familiar with an older API where the `ref` attribute is a string, like `"textInput"`, and the DOM node is accessed as `this.refs.textInput`. We advise against it because string refs have [some issues](https://github.com/facebook/react/pull/8333#issuecomment-271648615), are considered legacy, and **are likely to be removed in one of the future releases**. +如果你之前使用过 React,你可能了解过之前的 API 中的 string 类型的 ref 属性,例如 `"textInput"`。你可以通过 `this.refs.textInput` 来访问 DOM 节点。我们不建议使用它,因为 string 类型的 refs 存在 [一些问题](https://github.com/facebook/react/pull/8333#issuecomment-271648615)。它已过时并可能会在未来的版本被移除。 -> Note +> 注意 > -> If you're currently using `this.refs.textInput` to access refs, we recommend using either the [callback pattern](#callback-refs) or the [`createRef` API](#creating-refs) instead. +> 如果你目前还在使用 `this.refs.textInput` 这种方式访问 refs ,我们建议用[回调函数](#callback-refs)或 [`createRef` API](#creating-refs) 的方式代替。 -### Caveats with callback refs {#caveats-with-callback-refs} +### 关于回调 refs 的警告 {#caveats-with-callback-refs} -If the `ref` callback is defined as an inline function, it will get called twice during updates, first with `null` and then again with the DOM element. This is because a new instance of the function is created with each render, so React needs to clear the old ref and set up the new one. You can avoid this by defining the `ref` callback as a bound method on the class, but note that it shouldn't matter in most cases. +如果 `ref` 回调函数是以内联函数的方式定义的,在更新过程中它会被执行两次,第一次传入参数 `null`,然后第二次会传入参数 `DOM` 元素。这是因为在每次渲染时会创建一个新的函数实例,所以 React 清空旧的 ref 并且设置新的。通过将 ref 的回调函数定义成类的绑定函数的方式可以避免上述问题,但是大多数情况下它是无关紧要的。 \ No newline at end of file From c69794ae72e56ff3ac9eb0939e55ee459832be74 Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sat, 9 Mar 2019 10:28:31 +0800 Subject: [PATCH 02/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 8d4a34c5c5..997b617690 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -13,7 +13,7 @@ permalink: docs/refs-and-the-dom.html Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法中创建的 React 元素。 -在典型的 React 数据流中,[props](/docs/components-and-props.html) 是唯一一个让父组件与子组件交互的唯一方式。要修改一个子组件,你需要使用新的 props 来重新渲染它。 +在典型的 React 数据流中,[props](/docs/components-and-props.html) 是父组件与子组件交互的唯一方式。要修改一个子组件,你需要使用新的 props 来重新渲染它。 但是,在某些情况下,你需要在典型数据流之外强制修改子组件。被修改的子组件可能是一个 React 组件的实例,也可能是一个 DOM 元素。对于这两种情况,React 都提供了解决办法。 ### 何时使用 Refs {#when-to-use-refs} @@ -288,4 +288,4 @@ class Parent extends React.Component { ### 关于回调 refs 的警告 {#caveats-with-callback-refs} -如果 `ref` 回调函数是以内联函数的方式定义的,在更新过程中它会被执行两次,第一次传入参数 `null`,然后第二次会传入参数 `DOM` 元素。这是因为在每次渲染时会创建一个新的函数实例,所以 React 清空旧的 ref 并且设置新的。通过将 ref 的回调函数定义成类的绑定函数的方式可以避免上述问题,但是大多数情况下它是无关紧要的。 \ No newline at end of file +如果 `ref` 回调函数是以内联函数的方式定义的,在更新过程中它会被执行两次,第一次传入参数 `null`,然后第二次会传入参数 `DOM` 元素。这是因为在每次渲染时会创建一个新的函数实例,所以 React 清空旧的 ref 并且设置新的。通过将 ref 的回调函数定义成类的绑定函数的方式可以避免上述问题,但是大多数情况下它是无关紧要的。 From 6650044cdfc0dd13c8cbd07974192be11e0b4f00 Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sat, 9 Mar 2019 10:28:45 +0800 Subject: [PATCH 03/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 997b617690..1db50aee10 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -34,7 +34,7 @@ Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法 > 注意: > -> 下面的例子已经更新为使用在 React 16.3 版本引入的 `React.createRef()` API。如果你正在使用一个较早的 Release 版本的 React,我们推荐你使用 [回调形式的 refs](#callback-refs)。 +> 下面的例子已经更新为使用在 React 16.3 版本引入的 `React.createRef()` API。如果你正在使用一个较早版本的 React,我们推荐你使用[回调形式的 refs](#callback-refs)。 ### 创建 Refs {#creating-refs} From 2a3fed2cea51b0b27d70aae3d42a8f8d24bcb775 Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:44:15 +0800 Subject: [PATCH 04/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 1db50aee10..4b4d5b37a3 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -131,7 +131,7 @@ class AutoFocusTextInput extends React.Component { } ``` -请注意,这仅在 `CustomTextInput` 声明为类时才有效: +请注意,这仅在 `CustomTextInput` 声明为 class 时才有效: ```js{1} class CustomTextInput extends React.Component { From 8d9b2a86160b1197ba77b9b733e6834ba17737ff Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:44:36 +0800 Subject: [PATCH 05/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 4b4d5b37a3..dae0e1ecd1 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -206,7 +206,7 @@ function CustomTextInput(props) { React 也支持另一种设置 refs 的方式,称为“回调 refs”。它能助你更精细地控制合适 refs 被设置和解除。 -不同于传递 `createRef()` 创建的 `ref` 属性,你会传递一个函数。这个函数中接受 React 组件实例或 HTML DOM 元素作为参数,以存储它们并使它们能被其他地方访问。 +不同于传递 `createRef()` 创建的 `ref` 属性,你会传递一个函数。这个函数中接受 React 组件实例或 HTML DOM 元素作为参数,以使它们能在其他地方被存储和访问。 下面的例子描述了一个通用的范例:使用 `ref` 回调函数,在实例的属性中存储对 DOM 节点的引用。 From a92229ba9dc04fda17a27b67a79ac848e35d91c4 Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:45:00 +0800 Subject: [PATCH 06/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index dae0e1ecd1..1a7ae39703 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -30,7 +30,7 @@ Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法 ### 勿过度使用 Refs {#dont-overuse-refs} -你可能首先会想到使用 refs 在你的 app 中“让事情发生”。如果是这种情况,请花一点时间,认真再考虑一下 state 属性应该被安排在哪个组件层中。通常你会想明白,让更高的组件层级拥有这个 state,是更恰当的。查看 [Lifting State Up](/docs/lifting-state-up.html) 以获取更多有关示例。 +你可能首先会想到使用 refs 在你的 app 中“让事情发生”。如果是这种情况,请花一点时间,认真再考虑一下 state 属性应该被安排在哪个组件层中。通常你会想明白,让更高的组件层级拥有这个 state,是更恰当的。查看 [状态提升](/docs/lifting-state-up.html) 以获取更多有关示例。 > 注意: > From 2262eb5048ecb2e1bc3585d6ff2c66b06d24d5a9 Mon Sep 17 00:00:00 2001 From: PAMPANG Date: Sun, 24 Mar 2019 09:46:04 +0800 Subject: [PATCH 07/17] Update refs-and-the-dom.md remove extra lines --- content/docs/refs-and-the-dom.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 1a7ae39703..d8c4b23649 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -13,8 +13,7 @@ permalink: docs/refs-and-the-dom.html Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法中创建的 React 元素。 -在典型的 React 数据流中,[props](/docs/components-and-props.html) 是父组件与子组件交互的唯一方式。要修改一个子组件,你需要使用新的 props 来重新渲染它。 -但是,在某些情况下,你需要在典型数据流之外强制修改子组件。被修改的子组件可能是一个 React 组件的实例,也可能是一个 DOM 元素。对于这两种情况,React 都提供了解决办法。 +在典型的 React 数据流中,[props](/docs/components-and-props.html) 是父组件与子组件交互的唯一方式。要修改一个子组件,你需要使用新的 props 来重新渲染它。但是,在某些情况下,你需要在典型数据流之外强制修改子组件。被修改的子组件可能是一个 React 组件的实例,也可能是一个 DOM 元素。对于这两种情况,React 都提供了解决办法。 ### 何时使用 Refs {#when-to-use-refs} From 1bb64e21ec314f75f3381bc0c2dbf6aa916539bf Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:46:20 +0800 Subject: [PATCH 08/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index d8c4b23649..a49bfd6833 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -31,7 +31,7 @@ Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法 你可能首先会想到使用 refs 在你的 app 中“让事情发生”。如果是这种情况,请花一点时间,认真再考虑一下 state 属性应该被安排在哪个组件层中。通常你会想明白,让更高的组件层级拥有这个 state,是更恰当的。查看 [状态提升](/docs/lifting-state-up.html) 以获取更多有关示例。 -> 注意: +> 注意 > > 下面的例子已经更新为使用在 React 16.3 版本引入的 `React.createRef()` API。如果你正在使用一个较早版本的 React,我们推荐你使用[回调形式的 refs](#callback-refs)。 From 5afff6324aff9341849d5dd732bf550c3f3c3c43 Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:46:34 +0800 Subject: [PATCH 09/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index a49bfd6833..49012a4d85 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -37,7 +37,7 @@ Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法 ### 创建 Refs {#creating-refs} -Refs是使用 `React.createRef()` 创建的,并通过 `ref` 属性附加到 React 元素。在构造组件时,通常将 Refs 分配给实例属性,以便可以在整个组件中引用它们。 +Refs 是使用 `React.createRef()` 创建的,并通过 `ref` 属性附加到 React 元素。在构造组件时,通常将 Refs 分配给实例属性,以便可以在整个组件中引用它们。 ```javascript{4,7} class MyComponent extends React.Component { From a5da0423d692cdf2c0417cd6dd4cd02aa60ef79a Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:46:58 +0800 Subject: [PATCH 10/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 49012a4d85..4a04002a1f 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -138,7 +138,7 @@ class CustomTextInput extends React.Component { } ``` -#### Refs 与函数式组件 {#refs-and-function-components} +#### Refs 与函数组件 {#refs-and-function-components} **你不能在函数式组件上使用 ref 属性**,因为它们没有实例: From 8932d6b71ee3027b583233c29f38b1d665ae2bca Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:48:31 +0800 Subject: [PATCH 11/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 4a04002a1f..5020f67e15 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -199,7 +199,7 @@ function CustomTextInput(props) { 如果你使用 16.2 或更低版本的 React,或者你需要比 ref 转发更高的灵活性,你可以使用[这个替代方案](https://gist.github.com/gaearon/1a018a023347fe1c2476073330cc5509)将 ref 作为特殊名字的 prop 直接传递。 -可能的话,我们不建议暴露 DOM 节点,但有时候它会成为救命稻草。注意这个方案需要你在子组件中增加一些代码。如果你对子组件的实现没有控制权的话,你剩下的选择是使用 [`findDOMNode()`](/docs/react-dom.html#finddomnode),但这是不推荐的,而且在[`严格模式`](/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage) 下被禁用。 +可能的话,我们不建议暴露 DOM 节点,但有时候它会成为救命稻草。注意这个方案需要你在子组件中增加一些代码。如果你对子组件的实现没有控制权的话,你剩下的选择是使用 [`findDOMNode()`](/docs/react-dom.html#finddomnode),但在[`严格模式`](/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage) 下已被废弃且不推荐使用。 ### 回调 Refs {#callback-refs} From 1ba6d6704c21a7da30fe94e13a6491007ef01f85 Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:48:45 +0800 Subject: [PATCH 12/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 5020f67e15..6bca677d1c 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -203,7 +203,7 @@ function CustomTextInput(props) { ### 回调 Refs {#callback-refs} -React 也支持另一种设置 refs 的方式,称为“回调 refs”。它能助你更精细地控制合适 refs 被设置和解除。 +React 也支持另一种设置 refs 的方式,称为“回调 refs”。它能助你更精细地控制何时 refs 被设置和解除。 不同于传递 `createRef()` 创建的 `ref` 属性,你会传递一个函数。这个函数中接受 React 组件实例或 HTML DOM 元素作为参数,以使它们能在其他地方被存储和访问。 From ed4fe9ed845a321d08ecd878913225e24503d63b Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:48:58 +0800 Subject: [PATCH 13/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 6bca677d1c..c1f881a8f5 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -251,7 +251,7 @@ class CustomTextInput extends React.Component { } ``` -React 将在组件挂载时,会调用 `ref` 回调函数并传入 DOM 元素,当卸载时调用它并传入 null。在 `componentDidMount` 或 `componentDidUpdate` 触发前,React 会保证refs 一定是最新的。 +React 将在组件挂载时,会调用 `ref` 回调函数并传入 DOM 元素,当卸载时调用它并传入 `null`。在 `componentDidMount` 或 `componentDidUpdate` 触发前,React 会保证 refs 一定是最新的。 你可以在组件间传递回调形式的 refs,就像你可以传递通过 `React.createRef()` 创建的对象 refs 一样。 From 36018bcacaa3a0381944165171960dbdbd2d3742 Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:49:08 +0800 Subject: [PATCH 14/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index c1f881a8f5..8e5d20b159 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -285,6 +285,6 @@ class Parent extends React.Component { > > 如果你目前还在使用 `this.refs.textInput` 这种方式访问 refs ,我们建议用[回调函数](#callback-refs)或 [`createRef` API](#creating-refs) 的方式代替。 -### 关于回调 refs 的警告 {#caveats-with-callback-refs} +### 关于回调 refs 的说明 {#caveats-with-callback-refs} 如果 `ref` 回调函数是以内联函数的方式定义的,在更新过程中它会被执行两次,第一次传入参数 `null`,然后第二次会传入参数 `DOM` 元素。这是因为在每次渲染时会创建一个新的函数实例,所以 React 清空旧的 ref 并且设置新的。通过将 ref 的回调函数定义成类的绑定函数的方式可以避免上述问题,但是大多数情况下它是无关紧要的。 From 8a285c34f271d7f3a1367bbbed98a68037ac558d Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:49:54 +0800 Subject: [PATCH 15/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 8e5d20b159..32b4c67079 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -287,4 +287,4 @@ class Parent extends React.Component { ### 关于回调 refs 的说明 {#caveats-with-callback-refs} -如果 `ref` 回调函数是以内联函数的方式定义的,在更新过程中它会被执行两次,第一次传入参数 `null`,然后第二次会传入参数 `DOM` 元素。这是因为在每次渲染时会创建一个新的函数实例,所以 React 清空旧的 ref 并且设置新的。通过将 ref 的回调函数定义成类的绑定函数的方式可以避免上述问题,但是大多数情况下它是无关紧要的。 +如果 `ref` 回调函数是以内联函数的方式定义的,在更新过程中它会被执行两次,第一次传入参数 `null`,然后第二次会传入参数 DOM 元素。这是因为在每次渲染时会创建一个新的函数实例,所以 React 清空旧的 ref 并且设置新的。通过将 ref 的回调函数定义成 class 的绑定函数的方式可以避免上述问题,但是大多数情况下它是无关紧要的。 From 7098e2cf7186654fe35a5bd885d0ded8e2395da0 Mon Sep 17 00:00:00 2001 From: PAMPANG Date: Sun, 24 Mar 2019 09:51:40 +0800 Subject: [PATCH 16/17] Update refs-and-the-dom.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit translate `function component` as `函数组件` instead of `函数式组件` --- content/docs/refs-and-the-dom.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 32b4c67079..08ca1a7a81 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -63,7 +63,7 @@ ref 的值根据节点的类型而有所不同: - 当 `ref` 属性用于 HTML 元素时,构造函数中使用 `React.createRef()` 创建的 `ref` 接收底层 DOM 元素作为其 `current` 属性。 - 当 `ref` 属性用于自定义 class 组件时,`ref` 对象接收组件的挂载实例作为其 `current` 属性。 -- **你不能在函数式组件上使用 `ref` 属性**,因为他们没有实例。 +- **你不能在函数组件上使用 `ref` 属性**,因为他们没有实例。 以下例子说明了这些差异。 @@ -140,7 +140,7 @@ class CustomTextInput extends React.Component { #### Refs 与函数组件 {#refs-and-function-components} -**你不能在函数式组件上使用 ref 属性**,因为它们没有实例: +**你不能在函数组件上使用 ref 属性**,因为它们没有实例: ```javascript{1,8,13} function MyFunctionComponent() { @@ -163,7 +163,7 @@ class Parent extends React.Component { 如果你需要使用 ref,你应该将组件转化为一个 class,就像当你需要使用生命周期钩子或 state 时一样。 -不管怎样,你可以**在函数式组件内部使用 `ref` 属性**,只要它指向一个 DOM 元素或 class 组件: +不管怎样,你可以**在函数组件内部使用 `ref` 属性**,只要它指向一个 DOM 元素或 class 组件: ```javascript{2,3,6,13} function CustomTextInput(props) { @@ -193,7 +193,7 @@ function CustomTextInput(props) { 在极少数情况下,你可能希望在父组件中引用子节点的 DOM 节点。通常不建议这样做,因为它会打破组件的封装,但它偶尔可用于触发焦点或测量子 DOM 节点的大小或位置。 -虽然你可以[向子组件添加 ref](#adding-a-ref-to-a-class-component),但这不是一个理想的解决方案,因为你只能获取组件实例而不是 DOM 节点。并且,它还在函数式组件上无效。 +虽然你可以[向子组件添加 ref](#adding-a-ref-to-a-class-component),但这不是一个理想的解决方案,因为你只能获取组件实例而不是 DOM 节点。并且,它还在函数组件上无效。 如果你使用 16.3 或更高版本的 React, 这种情况下我们推荐使用 [ref 转发](/docs/forwarding-refs.html)。**Ref 转发使组件可以像暴露自己的 ref 一样暴露子组件的 ref**。关于怎样对父组件暴露子组件的 DOM 节点,在 [ref 转发文档](/docs/forwarding-refs.html#forwarding-refs-to-dom-components)中有一个详细的例子。 From ea86077cdadd7eee64903f6e63f25a62f087d4f1 Mon Sep 17 00:00:00 2001 From: Yuqing Chen Date: Sun, 24 Mar 2019 09:56:20 +0800 Subject: [PATCH 17/17] Update content/docs/refs-and-the-dom.md Co-Authored-By: pampang --- content/docs/refs-and-the-dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index 08ca1a7a81..7c8f981779 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -277,7 +277,7 @@ class Parent extends React.Component { 在上面的例子中,`Parent` 把它的 refs 回调函数当作 `inputRef` props 传递给了 `CustomTextInput`,而且 `CustomTextInput` 把相同的函数作为特殊的 `ref` 属性传递给了 ``。结果是,在 `Parent` 中的 `this.inputElement` 会被设置为与 `CustomTextInput` 中的 `input` 元素相对应的 DOM 节点。 -### 旧版 API:String 类型的 Refs {#legacy-api-string-refs} +### 过时 API:String 类型的 Refs {#legacy-api-string-refs} 如果你之前使用过 React,你可能了解过之前的 API 中的 string 类型的 ref 属性,例如 `"textInput"`。你可以通过 `this.refs.textInput` 来访问 DOM 节点。我们不建议使用它,因为 string 类型的 refs 存在 [一些问题](https://github.com/facebook/react/pull/8333#issuecomment-271648615)。它已过时并可能会在未来的版本被移除。