Skip to content

Commit

Permalink
Add improved warning and docs for 'refs must have owner' in Fiber
Browse files Browse the repository at this point in the history
**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 facebook#9962
Related to facebook#8854
  • Loading branch information
flarnie committed Jul 13, 2017
1 parent b23d326 commit 8cb9df3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
8 changes: 7 additions & 1 deletion docs/warnings/refs-must-have-owner.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ layout: single
permalink: warnings/refs-must-have-owner.html
---

You are probably here because you got the following error messages:
You are probably here because you got one of the following error messages:

*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.
Expand Down
12 changes: 12 additions & 0 deletions src/renderers/shared/fiber/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ function coerceRef(current: Fiber | null, element: ReactElement) {
};
ref._stringRef = stringRef;
return ref;
} else {
invariant(
typeof mixedRef === 'string',
'Expected ref to be a function or a string.',
);
invariant(
element._owner,
'Element ref was specified as a string (%s) but no owner was ' +
'set. You may have multiple copies of React loaded. ' +
'(details: https://fb.me/react-refs-must-have-owner).',
mixedRef,
);
}
}
return mixedRef;
Expand Down
6 changes: 0 additions & 6 deletions src/renderers/shared/fiber/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,6 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
function commitAttachRef(finishedWork: Fiber) {
const ref = finishedWork.ref;
if (ref !== null) {
invariant(
typeof ref === 'function',
'commitAttachRef(...): Expected ref to be a function. ' +
'You may have multiple copies of React loaded. ' +
'(details: https://fb.me/react-expected-ref-to-be-function).',
);
const instance = finishedWork.stateNode;
switch (finishedWork.tag) {
case HostComponent:
Expand Down

0 comments on commit 8cb9df3

Please sign in to comment.