-
Notifications
You must be signed in to change notification settings - Fork 47.7k
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
Improve error message thrown in Fiber with multiple copies of React #10151
Conversation
Would this invariant make more sense as an
That way only that one piece of code needs to care about string refs. |
I think that makes sense @spicyj - if we put the 'invariant' there then we could even use basically the same error message and doc page, right? |
Yeah. I would tweak it a bit: "Expected ref to be a function" is a little confusing if you pass a string ref because it requires the reader to understand that React converts the string to a function behind the scenes, which is an implementation detail. Something like "Element ref was specified as a string (myRef) but no owner was set." could be better. In the case that it's neither string nor function, you could say "Expected ref to be a function or a string". |
Maybe also let's not include |
Ah - I meant we could reuse the warning wording and docs from the old warning that gets thrown in 15.* - https://facebook.github.io/react/warnings/refs-must-have-owner.html But I think you are right, we should give a specific error for this case, and probably a new docs page similar to https://facebook.github.io/react/warnings/refs-must-have-owner.html I spent a while trying to write a test for this, and wasn't able to reproduce the error in the test environment. I was trying to use 'jestResetModules' to load and use a second copy of React, and it seemed to work, but the error wasn't triggered. This is what I tried: https://gist.github.com/flarnie/46f744f6981f1f35861ef4cdeba67af1 Since we didn't have a test for the old warning (Refs must have owner) we could merge this without a test. Writing a fixture that reproduces the error seems easier, but I don't want to commit to the overhead of checking it manually. |
I can't find any way to trigger the case where 'In the case that it's neither string nor function, you could say "Expected ref to be a function or a string".' so I'm not putting too much thought into that message, but I guess it makes sense to add that invariant since we do expect it to be a string or a function. |
What happens if you do ref={17} or ref={{abc: true}}? |
It doesn't complain for |
4c15b50
to
89e341b
Compare
And now it has a test! Thank youuuuuu @gaearon 🏎️ |
Why do you end up splitting out the tests between stack and fiber? |
I thought it would be more clear that way - looking now for patterns that are used currently. |
I'll update it to all be in one |
I guess I am confused why there needs to be a branch at all. Is the behavior not the same? |
**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 facebook#9962
**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
@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`
**what is the change?:** refactor test for 'multiple copies of React' to be simpler and remove some copypasta
**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.
For the record, we talked more about why there needs to be a different branch for 'fiber' and 'stack' versions of this behavior; the new warning is in fiber only, and the old warning shows up if you are using stack. The old warning will go away when we delete stack, but for now the test is covering both cases. |
7dbef6f
to
34f0b4d
Compare
what is the change?:
get parity with older versions of React.
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:
yarn test src/renderers/__tests__/multiple-copies-of-react-test.js
REACT_JEST_USE_FIBER=1 yarn run test src/renderers/__tests__/multiple-copies-of-react-test.js
issue:
Fixes #9962
Related to #8854