question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React has detected a change in the order of Hooks called by EmotionCssPropInternal

See original GitHub issue

Current behavior:

When using a conditional css which consumes theme, the following React error is produced when the condition changes from false to true:

React has detected a change in the order of Hooks called by EmotionCssPropInternal

To reproduce:

https://codesandbox.io/s/emotion-issue-template-forked-ettgx?file=/src/index.js

In particular, the line

<Title css={toggle && toggledCss}>

when toggle changes from false to true, the error above is produced. It only happens the first time it’s toggled, I assume this is because the css or resulting wrapped component is cached somewhere? – not sure and not sure if relevant. I did some digging in Emotion and found the offending line to be this one. This line also hinted at a potential solution:

<Title css={[toggle && toggledCss]}>

Now… I’m not really sure which styles of css usage are supported. The documentation is lacking in this regard and much of our usage is inferred from TypeScript types. So really, my question is: if this is invalid, how should I write something like this? A less contrived example might be conditionally applying hover, active, etc states depending on whether or not a button is disabled.

Expected behavior:

If this style is in fact supported, no React warning should be produced.

Environment information:

  • react version: 16.8.6+
  • @emotion/react version: 11.0+

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
Andaristcommented, Jul 20, 2021

Sorry for the delay - I hope to merge the mentioned PR this week. Didn’t have much time last week to get to this.

3reactions
AlexanderArvidssoncommented, Jul 19, 2021

I just ran into this issue in our platform, except for me the same problem occurs regardless if I use an array and always on initial render, even if the condition does not change (I tested with a constant).

Quickly looking at the line that @tills13 found, it seems to me that this line breaks the first Rules of Hooks, namely that hooks should never be called conditionally.

Looking forward to get the PR merged, I will make the change as a patch and try it out myself as well. Edit: Yep, seems like it did the trick, still curious if this was done merely as a premature optimization or if there is a big performance difference.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"React has detected a change in the order of Hooks" but ...
I ran into this same error message in a component I was writing due to use of short-circuiting logic. This resulted in an...
Read more >
React has detected a change in the order of Hooks called
The error "React has detected a change in the order of Hooks called" occurs when we use hooks in conditionals or conditionally return...
Read more >
How to overcome warning 'React has detected a change in ...
A look at a functional component that under non-obvious circumstances triggers this React Hooks warning.
Read more >
eact has detected a change in the order of hooks called by
Im getting this error, Warning: React has detected a change in the order of Hooks called by Thread. But i can' see the...
Read more >
React has detected a change in the order of Hooks" but Hooks ...
The problem is that you're calling Event.Event() directly, even though it is a react component. That causes react to treat the hook calls...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found