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

See original GitHub issue

Related to the experiment of moving to Recoil, we’re seeing the above error message next to a Cannot assign to read only property error message. What we’re doing is basically this:

const setState = useSetRecoilState(someAtomWithMutability)
useEffect(() => {
  setState(s => {
    s.close()
    return null
  })
}, [])

The atom is mutable and s.close() is mutating in some way. That seems to cause issues.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
BenjaBobscommented, Mar 11, 2021

Assuming s is some kind of connnection or short lived object, I don’t think it’s a good idea to put that directly in recoil state. I would instead have an external object that manages s, and then store whatever data s produces in recoil state.

0reactions
rpggiocommented, Mar 13, 2022

A note for anyone else who encounters ‘React has detected a change in the order of Hooks’: You will get this error when you accidentally use a React hook inside of Recoil code (such as in selector get implementation). It’s easy to inadvertently use useRecoilValue(otherValue) when you should be doing get(otherValue). The linter should be picking this up, but mine isn’t.

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 >
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 >
React has detected a change in the order of Hooks called ...
I see following error when I render a list. There are no if/else in the component which usually lead to such problem.
Read more >
Invalid Hook Call Warning
Hooks can only be called inside the body of a function component. There are three common reasons you might be seeing it: You...
Read more >
react has detected a change in the order of hooks called - ...
This solves the error because we have to ensure that React hooks are called in the same order each time a component renders....
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