Skip to content

Commit 91e3357

Browse files
jrr997zhangjianxiongYucohnyXleine
authored
docs(cn): translate reference/react/createRef into Chinese (#1138)
Co-authored-by: zhangjianxiong <[email protected]> Co-authored-by: Yucohny <[email protected]> Co-authored-by: Xleine <[email protected]>
1 parent ead06dd commit 91e3357

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

src/content/reference/react/createRef.md

+26-26
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ title: createRef
44

55
<Pitfall>
66

7-
`createRef` is mostly used for [class components.](/reference/react/Component) Function components typically rely on [`useRef`](/reference/react/useRef) instead.
7+
`createRef` 主要用于 [class 组件](/reference/react/Component)。而函数组件通常使用 [`useRef`](/reference/react/useRef)
88

99
</Pitfall>
1010

1111
<Intro>
1212

13-
`createRef` creates a [ref](/learn/referencing-values-with-refs) object which can contain arbitrary value.
13+
`createRef` 创建一个 [ref](/learn/referencing-values-with-refs) 对象,该对象可以包含任意值。
1414

1515
```js
1616
class MyInput extends Component {
@@ -25,11 +25,11 @@ class MyInput extends Component {
2525

2626
---
2727

28-
## Reference {/*reference*/}
28+
## 参考 {/*reference*/}
2929

3030
### `createRef()` {/*createref*/}
3131

32-
Call `createRef` to declare a [ref](/learn/referencing-values-with-refs) inside a [class component.](/reference/react/Component)
32+
[class 组件](/reference/react/Component) 中调用 `createRef` 来声明一个 [ref](/learn/referencing-values-with-refs)
3333

3434
```js
3535
import { createRef, Component } from 'react';
@@ -40,31 +40,31 @@ class MyComponent extends Component {
4040
// ...
4141
```
4242
43-
[See more examples below.](#usage)
43+
[请看下方更多示例](#usage)
4444
45-
#### Parameters {/*parameters*/}
45+
#### 参数 {/*parameters*/}
4646
47-
`createRef` takes no parameters.
47+
`createRef` 不接受任何参数。
4848
49-
#### Returns {/*returns*/}
49+
#### 返回值 {/*returns*/}
5050
51-
`createRef` returns an object with a single property:
51+
`createRef` 返回一个对象,该对象只有一个属性:
5252
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.
53+
* `current`:初始值为 `null`,你可以稍后设置为其他内容。如果你把 ref 对象作为 JSX 节点的 `ref` 属性传递给 React,React 将设置其 `current` 属性。
5454
55-
#### Caveats {/*caveats*/}
55+
#### 注意事项 {/*caveats*/}
5656
57-
* `createRef` always returns a *different* object. It's equivalent to writing `{ current: null }` yourself.
58-
* In a function component, you probably want [`useRef`](/reference/react/useRef) instead which always returns the same object.
59-
* `const ref = useRef()` is equivalent to `const [ref, _] = useState(() => createRef(null))`.
57+
* `createRef` 总是返回一个 **不同的** 对象。这相当于你自己编写了 `{ current: null }`
58+
* 在函数组件中,你可能想要使用 [`useRef`](/reference/react/useRef),因为它始终返回相同的对象。
59+
* `const ref = useRef()` 等同于 `const [ref, _] = useState(() => createRef(null))`
6060
6161
---
6262
63-
## Usage {/*usage*/}
63+
## 用法 {/*usage*/}
6464
65-
### Declaring a ref in a class component {/*declaring-a-ref-in-a-class-component*/}
65+
### class 组件中声明 ref {/*declaring-a-ref-in-a-class-component*/}
6666
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 字段:
6868
6969
```js {4}
7070
import { Component, createRef } from 'react';
@@ -76,7 +76,7 @@ class Form extends Component {
7676
}
7777
```
7878
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:
79+
如果你现在将 `ref={this.inputRef}` 传递给 JSX 中的 `<input>`,React 将把 input 的 DOM 节点赋值给 `this.inputRef.current`。例如,下面这段代码演示了如何创建一个按钮,点击该按钮会将焦点聚焦在输入框上:
8080
8181
<Sandpack>
8282
@@ -95,7 +95,7 @@ export default class Form extends Component {
9595
<>
9696
<input ref={this.inputRef} />
9797
<button onClick={this.handleClick}>
98-
Focus the input
98+
聚焦这个输入框
9999
</button>
100100
</>
101101
);
@@ -107,17 +107,17 @@ export default class Form extends Component {
107107
108108
<Pitfall>
109109
110-
`createRef` is mostly used for [class components.](/reference/react/Component) Function components typically rely on [`useRef`](/reference/react/useRef) instead.
110+
`createRef` 主要用于 [class 组件](/reference/react/Component)。而函数组件通常使用 [`useRef`](/reference/react/useRef)
111111
112112
</Pitfall>
113113
114114
---
115115
116-
## Alternatives {/*alternatives*/}
116+
## 替代方案 {/*alternatives*/}
117117
118-
### 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*/}
119119
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 组件,以下是如何将它们转换为函数组件。这是原始代码:
121121
122122
<Sandpack>
123123
@@ -136,7 +136,7 @@ export default class Form extends Component {
136136
<>
137137
<input ref={this.inputRef} />
138138
<button onClick={this.handleClick}>
139-
Focus the input
139+
聚焦这个输入框
140140
</button>
141141
</>
142142
);
@@ -146,7 +146,7 @@ export default class Form extends Component {
146146
147147
</Sandpack>
148148
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` 的调用:
150150
151151
<Sandpack>
152152
@@ -164,7 +164,7 @@ export default function Form() {
164164
<>
165165
<input ref={inputRef} />
166166
<button onClick={handleClick}>
167-
Focus the input
167+
聚焦这个输入框
168168
</button>
169169
</>
170170
);

0 commit comments

Comments
 (0)