-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
React 18: having Invalid hook call problem with rendering app wrapped by React.StrictMode #21848
Comments
Thanks for the feedback. I suspect that your package manager installed both React 18 and 17. Multiple React versions is one of the reasons for the "invalid hook call" error message. This might be due to dependencies requiring React 17 via peer dependencies. What package manager are you using? |
Hi @julia-dizhak 👋 Can you help us by providing a link to a CodeSandbox (https://codesandbox.io/s/new), a repository on GitHub, or a minimal code example that reproduces the problem? The info provided isn't sufficient to rule out something like what @eps1lon mentions above. |
hi @bvaughn thanks for taking care |
hi @eps1lon thanks for support |
Thanks for the repro! |
That looks like it's caused by a dev-only logic from styled-components (styled-components/styled-components#3394, styled-components/styled-components#3409) since the reported issue is already reproducible with import styled from "styled-components";
const StyledPeopleIcon = styled("svg")`
margin: 10px;
`; Closing since it's caused by a 3rd party library. The issue is already fixed in styled-components/styled-components#3521 but not released yet. |
Hmm, wait, something isn't right here. The comments in styled-components/styled-components#3521 claim: // If a hook is called outside of a component:
// React 17 and earlier throw an error
// React 18 and above use console.error I don't remember a change like this. I expect we still have a production invariant/error in cases of invalid Hook call. Can we investigate what the difference is? |
We throw here: react/packages/react-reconciler/src/ReactFiberHooks.new.js Lines 293 to 301 in cae6350
There is a DEV-only console.error here: react/packages/react/src/ReactHooks.js Lines 26 to 40 in cae6350
But it's supposed to throw right afterwards with null access (essentially, Are you saying it doesn't throw anymore? |
I'm pretty sure this was changed in #20604 since I saw the errors shortly after in integration tests with
It does throw but it's no longer clear why without also listening to the console. So the claim is a bit incomplete but sufficient for what we're trying to do: Checking if we're called from inside a function component requires different implementations for React 17 and 18 due to #20604 |
Ah ok. |
I have a problem with rendering app after migrating to 18 version
I have Invalid hook call warning in console
node is V14.16.1
installed react alpha via
yarn add react@alpha react-dom@alpha
Dependencies
The text was updated successfully, but these errors were encountered: