You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.5 KiB

---
title: Refs Must Have Owner Warning
layout: single
permalink: warnings/refs-must-have-owner.html
---
Improve error message thrown in Fiber with multiple copies of React (#10151) * WIP Improve error message thrown in Fiber with multiple copies of React **what is the change?:** Adding an 'invariant' with detailed error message for the problem that occurs when you load two copies of React with the Fiber reconciler. WIP: - Is there any other likely cause for this error besides two copies of React? - How can we make the message more clear? Still TODO: - Write a unit test - Write a documentation page and create the link to the page, similar to https://facebook.github.io/react/warnings/refs-must-have-owner.html It would also be nice to have a page with instructions on how to fix common cases of accidental double-loading of React, but we can open a separate issue on that and let the community do it. **why make this change?:** This error comes up relatively often and we want to make things clear when it happens in v16.0+ **test plan:** WIP **issue:** Fixes #9962 * Add improved warning and docs for 'refs must have owner' in Fiber **what is the change?:** - Added warning in the place where this error is thrown in Fiber, to get parity with older versions of React. - Updated docs to mention new error message as well as old one. I started to write a new docs page for the new error, and realized the content was the same as the old one. So then I just updated the existing error page. **why make this change?:** We want to avoid confusion when this error is thrown from React v16. **test plan:** - manually inspected docs page - manually tested in a CRA to trigger the error message (Flarnie will insert screenshots) **issue:** Fixes #9962 Related to #8854 * Add test for the informative warning around multiple react copies @gaearon debugged the test for this and now it works!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!! :) **what is the change?:** We now test for the warning that the previous commits add in Fiber, and also test for the old warning in the stack reconciler. **why make this change?:** This is an especially important warning, and we want to know that it will fire correctly. **test plan:** `yarn test src/renderers/__tests__/multiple-copies-of-react-test.js` `REACT_DOM_JEST_USE_FIBER=1 yarn test src/renderers/__tests__/multiple-copies-of-react-test.js` * Fix up test for 'multiple copies of react' **what is the change?:** refactor test for 'multiple copies of React' to be simpler and remove some copypasta * run prettier * Fix conditionals in 'multiple copies of react' test **what is the change?:** When moving the 'fiber' and 'non-fiber' conditions from two assertions into one, we copy pasted the wrong message into the 'fiber' condition. This wasn't caught because we were using an outdated name for the 'fiber' constant when running the tests locally with fiber enabled. This fixes the copy-paste error and we now are actually running the tests with fiber enabled locally. * Run scripts/fiber/record-tests
8 years ago
You are probably here because you got one of the following error messages:
Improve error message thrown in Fiber with multiple copies of React (#10151) * WIP Improve error message thrown in Fiber with multiple copies of React **what is the change?:** Adding an 'invariant' with detailed error message for the problem that occurs when you load two copies of React with the Fiber reconciler. WIP: - Is there any other likely cause for this error besides two copies of React? - How can we make the message more clear? Still TODO: - Write a unit test - Write a documentation page and create the link to the page, similar to https://facebook.github.io/react/warnings/refs-must-have-owner.html It would also be nice to have a page with instructions on how to fix common cases of accidental double-loading of React, but we can open a separate issue on that and let the community do it. **why make this change?:** This error comes up relatively often and we want to make things clear when it happens in v16.0+ **test plan:** WIP **issue:** Fixes #9962 * Add improved warning and docs for 'refs must have owner' in Fiber **what is the change?:** - Added warning in the place where this error is thrown in Fiber, to get parity with older versions of React. - Updated docs to mention new error message as well as old one. I started to write a new docs page for the new error, and realized the content was the same as the old one. So then I just updated the existing error page. **why make this change?:** We want to avoid confusion when this error is thrown from React v16. **test plan:** - manually inspected docs page - manually tested in a CRA to trigger the error message (Flarnie will insert screenshots) **issue:** Fixes #9962 Related to #8854 * Add test for the informative warning around multiple react copies @gaearon debugged the test for this and now it works!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!! :) **what is the change?:** We now test for the warning that the previous commits add in Fiber, and also test for the old warning in the stack reconciler. **why make this change?:** This is an especially important warning, and we want to know that it will fire correctly. **test plan:** `yarn test src/renderers/__tests__/multiple-copies-of-react-test.js` `REACT_DOM_JEST_USE_FIBER=1 yarn test src/renderers/__tests__/multiple-copies-of-react-test.js` * Fix up test for 'multiple copies of react' **what is the change?:** refactor test for 'multiple copies of React' to be simpler and remove some copypasta * run prettier * Fix conditionals in 'multiple copies of react' test **what is the change?:** When moving the 'fiber' and 'non-fiber' conditions from two assertions into one, we copy pasted the wrong message into the 'fiber' condition. This wasn't caught because we were using an outdated name for the 'fiber' constant when running the tests locally with fiber enabled. This fixes the copy-paste error and we now are actually running the tests with fiber enabled locally. * Run scripts/fiber/record-tests
8 years ago
*React 16.0.0+*
> Warning:
>
> 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).
*earlier versions of React*
> Warning:
>
> 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.
This usually means one of three things:
- You are trying to add a `ref` to a function component.
- You are trying to add a `ref` to an element that is being created outside of a component's render() function.
- You have multiple (conflicting) copies of React loaded (eg. due to a misconfigured npm dependency)
## Refs on Function Components {#refs-on-function-components}
If `<Foo>` is a function component, you can't add a ref to it:
```js
// Doesn't work if Foo is a function!
<Foo ref={foo} />
```
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).
## Strings Refs Outside the Render Method {#strings-refs-outside-the-render-method}
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:
```js
// Doesn't work!
ReactDOM.render(<App ref="app" />, el);
```
Try rendering this component inside of a new top-level component which will hold the ref. Alternatively, you may use a callback ref:
```js
let app;
ReactDOM.render(
<App ref={inst => {
app = inst;
}} />,
el
);
```
Consider if you [really need a ref](/docs/refs-and-the-dom.html#when-to-use-refs) before using this approach.
## Multiple copies of React {#multiple-copies-of-react}
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.
If you are using npm... `npm ls` or `npm ls react` might help illuminate.