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

Conversation

flarnie
Copy link
Contributor

@flarnie flarnie commented Jul 12, 2017

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:

  • 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
  • manually inspected docs page
  • manually tested in a CRA to trigger the error message

screen shot 2017-07-12 at 12 15 22 pm

screen shot 2017-07-12 at 11 52 07 am

issue:
Fixes #9962
Related to #8854

@sophiebits
Copy link
Contributor

Would this invariant make more sense as an else to this if?

That way only that one piece of code needs to care about string refs.

@flarnie
Copy link
Contributor Author

flarnie commented Jul 12, 2017

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?

@sophiebits
Copy link
Contributor

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".

@gaearon
Copy link
Collaborator

gaearon commented Jul 12, 2017

Maybe also let's not include commitAttachRef(...) there? It's not very useful to us since there's only one such invariant, and it's confusing to the user since it shows an internal method name as if they were expected to know what it means.

@flarnie
Copy link
Contributor Author

flarnie commented Jul 12, 2017

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.

@flarnie
Copy link
Contributor Author

flarnie commented Jul 12, 2017

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.

@sophiebits
Copy link
Contributor

What happens if you do ref={17} or ref={{abc: true}}?

@flarnie
Copy link
Contributor Author

flarnie commented Jul 12, 2017

It doesn't complain for ref={17}, ref={{abc: true}}. The only thing I found to pass to ref that throws is ref={Function} and I think that is bad syntax, not really worried about someone doing that.

@flarnie flarnie force-pushed the improveRefNotFunctionWarning branch from 4c15b50 to 89e341b Compare July 12, 2017 18:53
@flarnie flarnie changed the title WIP Improve error message thrown in Fiber with multiple copies of React Improve error message thrown in Fiber with multiple copies of React Jul 12, 2017
@flarnie
Copy link
Contributor Author

flarnie commented Jul 12, 2017

And now it has a test! Thank youuuuuu @gaearon 🏎️

@sophiebits
Copy link
Contributor

Why do you end up splitting out the tests between stack and fiber?

@flarnie
Copy link
Contributor Author

flarnie commented Jul 12, 2017

I thought it would be more clear that way - looking now for patterns that are used currently.

@flarnie
Copy link
Contributor Author

flarnie commented Jul 12, 2017

I'll update it to all be in one it(... assertion.

@sophiebits
Copy link
Contributor

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.
@flarnie
Copy link
Contributor Author

flarnie commented Jul 13, 2017

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.

@flarnie flarnie force-pushed the improveRefNotFunctionWarning branch from 7dbef6f to 34f0b4d Compare July 13, 2017 16:42
@flarnie flarnie merged commit 309412d into facebook:master Jul 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants