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
Copy file name to clipboardExpand all lines: src/content/reference/react/createRef.md
+26-26
Original file line number
Diff line number
Diff line change
@@ -4,13 +4,13 @@ title: createRef
4
4
5
5
<Pitfall>
6
6
7
-
`createRef`is mostly used for [class components.](/reference/react/Component) Function components typically rely on [`useRef`](/reference/react/useRef) instead.
@@ -40,31 +40,31 @@ class MyComponent extends Component {
40
40
// ...
41
41
```
42
42
43
-
[See more examples below.](#usage)
43
+
[请看下方更多示例](#usage)。
44
44
45
-
#### Parameters {/*parameters*/}
45
+
#### 参数 {/*parameters*/}
46
46
47
-
`createRef`takes no parameters.
47
+
`createRef`不接受任何参数。
48
48
49
-
#### Returns {/*returns*/}
49
+
#### 返回值 {/*returns*/}
50
50
51
-
`createRef`returns an object with a single property:
51
+
`createRef`返回一个对象,该对象只有一个属性:
52
52
53
-
* `current`: Initially, it's set to the `null`. You can later set it to something else. If you pass the ref object to React as a `ref`attribute to a JSX node, React will set its `current`property.
### Declaring a ref in a class component {/*declaring-a-ref-in-a-class-component*/}
65
+
### 在 class 组件中声明 ref {/*declaring-a-ref-in-a-class-component*/}
66
66
67
-
To declare a ref inside a [class component,](/reference/react/Component) call `createRef`and assign its result to a class field:
67
+
要在 [class 组件](/reference/react/Component) 中声明一个 ref,请调用 `createRef`并将其结果分配给 class 字段:
68
68
69
69
```js {4}
70
70
import { Component, createRef } from'react';
@@ -76,7 +76,7 @@ class Form extends Component {
76
76
}
77
77
```
78
78
79
-
If you now pass `ref={this.inputRef}`to an `<input>` in your JSX, React will populate `this.inputRef.current` with the input DOM node. For example, here is how you make a button that focuses the input:
@@ -95,7 +95,7 @@ export default class Form extends Component {
95
95
<>
96
96
<input ref={this.inputRef} />
97
97
<button onClick={this.handleClick}>
98
-
Focus the input
98
+
聚焦这个输入框
99
99
</button>
100
100
</>
101
101
);
@@ -107,17 +107,17 @@ export default class Form extends Component {
107
107
108
108
<Pitfall>
109
109
110
-
`createRef`is mostly used for [class components.](/reference/react/Component) Function components typically rely on [`useRef`](/reference/react/useRef) instead.
### Migrating from a class with `createRef`to a function with `useRef` {/*migrating-from-a-class-with-createref-to-a-function-with-useref*/}
118
+
### 从使用 `createRef`的 class 组件迁移到使用 `useRef` 的函数组件 {/*migrating-from-a-class-with-createref-to-a-function-with-useref*/}
119
119
120
-
We recommend using function components instead of [class components](/reference/react/Component) in new code. If you have some existing class components using `createRef`, here is how you can convert them. This is the original code:
120
+
我们建议在新代码中使用函数组件而不是 [class 组件](/reference/react/Component)。如果你有一些使用了 `createRef` 的 class 组件,以下是如何将它们转换为函数组件。这是原始代码:
121
121
122
122
<Sandpack>
123
123
@@ -136,7 +136,7 @@ export default class Form extends Component {
136
136
<>
137
137
<input ref={this.inputRef} />
138
138
<button onClick={this.handleClick}>
139
-
Focus the input
139
+
聚焦这个输入框
140
140
</button>
141
141
</>
142
142
);
@@ -146,7 +146,7 @@ export default class Form extends Component {
146
146
147
147
</Sandpack>
148
148
149
-
When you [convert this component from a class to a function,](/reference/react/Component#alternatives) replace calls to `createRef` with calls to [`useRef`:](/reference/react/useRef)
149
+
当你 [将此组件从 class 组件转化为函数组件](/reference/react/Component#alternatives) 时,用 [`useRef`](/reference/react/useRef) 替换 `createRef` 的调用:
150
150
151
151
<Sandpack>
152
152
@@ -164,7 +164,7 @@ export default function Form() {
0 commit comments