Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message thrown in Fiber with multiple copies of React #10151

Merged
merged 7 commits into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions scripts/fiber/tests-passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,9 @@ src/renderers/__tests__/ReactUpdates-test.js
* does not re-render if state update is null
* synchronously renders hidden subtrees

src/renderers/__tests__/multiple-copies-of-react-test.js
* throws the "Refs must have owner" warning

src/renderers/__tests__/refs-destruction-test.js
* should remove refs when destroying the parent
* should remove refs when destroying the child
Expand Down
51 changes: 51 additions & 0 deletions src/renderers/__tests__/multiple-copies-of-react-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/

'use strict';

let React = require('react');
var ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
var ReactTestUtils = require('react-dom/test-utils');

class TextWithStringRef extends React.Component {
render() {
jest.resetModules();
React = require('react');
return (
<span ref="foo">
Hello world!
</span>
);
}
}

describe('when different React version is used with string ref', () => {
it('throws the "Refs must have owner" warning', () => {
if (ReactDOMFeatureFlags.useFiber) {
expect(() => {
ReactTestUtils.renderIntoDocument(<TextWithStringRef />);
}).toThrow(
'Element ref was specified as a string (foo) but no owner was set.' +
' You may have multiple copies of React loaded. (details: ' +
'https://fb.me/react-refs-must-have-owner).',
);
} else {
expect(() => {
ReactTestUtils.renderIntoDocument(<TextWithStringRef />);
}).toThrow(
'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 ' +
'(details: https://fb.me/react-refs-must-have-owner)',
);
}
});
});
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