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
You are probably here because you got one of the following error messages:
7
+
你能到这个页面很可能是因为你看到了以下错误信息:
8
8
9
9
*React 16.0.0+*
10
10
> Warning:
11
11
>
12
12
> Element ref was specified as a string (myRefName) but no owner was set. You may have multiple copies of React loaded. (details: https://fb.me/react-refs-must-have-owner).
13
13
14
-
*earlier versions of React*
14
+
*早期版本的 React*
15
15
> Warning:
16
16
>
17
17
> addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's `render` method, or you have multiple copies of React loaded.
18
18
19
-
This usually means one of three things:
19
+
这通常意味着以下三种原因之一:
20
20
21
-
-You are trying to add a `ref` to a function component.
22
-
-You are trying to add a `ref`to an element that is being created outside of a component's render() function.
23
-
-You have multiple (conflicting) copies of React loaded (eg. due to a misconfigured npm dependency)
21
+
-你试图给一个函数组件添加 `ref`;
22
+
-你试图把 `ref`添加到一个在 render() 函数之外生成的元素上;
23
+
-你加载了多个不同的(冲突的) React 副本(比方说是由配置错误的 npm 依赖所引起)。
24
24
25
-
## Refs on Function Components {#refs-on-function-components}
25
+
## 函数组件上的 Refs {#refs-on-function-components}
26
26
27
-
If`<Foo>`is a function component, you can't add a ref to it:
27
+
如果`<Foo>`是一个函数组件,那么你不能给它添加 ref:
28
28
29
29
```js
30
-
//Doesn't work if Foo is a function!
30
+
//如果 Foo 是一个函数,那么这样将不起作用!
31
31
<Foo ref={foo} />
32
32
```
33
33
34
-
If you need to add a ref to a component, convert it to a class first, or consider not using refs as they are [rarely necessary](/docs/refs-and-the-dom.html#when-to-use-refs).
This usually means that you're trying to add a ref to a component that doesn't have an owner (that is, was not created inside of another component's `render`method). For example, this won't work:
## Multiple copies of React {#multiple-copies-of-react}
59
+
## 多个 React 副本 {#multiple-copies-of-react}
60
60
61
-
Bower does a good job of deduplicating dependencies, but npm does not. If you aren't doing anything (fancy) with refs, there is a good chance that the problem is not with your refs, but rather an issue with having multiple copies of React loaded into your project. Sometimes, when you pull in a third-party module via npm, you will get a duplicate copy of the dependency library, and this can create problems.
0 commit comments