-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Invalid Hook Call in React #4220
Comments
I had the same problem, I fix it like this:
I think, you need to change your component.tsx, like this
|
I can reproduce this as well, however I only see one copy of react in your node_modules folder: @Mrahmani71 that also fixes it for me, which is wild! @natemoo-re anything odd that our JSX compilation step could be doing? |
@natemoo-re and I tracked this down to |
Hey Astro team I am also facing the same while using some basic hooks it will be great to get this fixes in coming days |
I also just encountered this issue. In addition to what was already reported here, I have found the following: // ❌ Exporting an anonymous function causes the warning to be logged
export default function () { /*...*/ }
// ✅ Exporting a named function works well
export default function Counter() { /*...*/ } |
Seeing this as well. What is interesting in my case is after using |
* Upgrade dependencies * Workaround: Invalid hook call withastro/astro#4220 (comment) * Run prettier
I'm also experiencing this error, using Astro version The error: Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app Ive done some trouble shooting and found that this error only occurs when using arrow functions. This works with no invalid hook call error: import { useState, useEffect } from 'react';
import { Sun, Moon } from 'react-feather';
export default function ThemeToggle() {
const [darkTheme, setDarkTheme] = useState(false);
useEffect(() => {
const body = document.querySelector('body');
if (darkTheme) {
body?.classList.add('dark');
} else {
body?.classList.remove('dark');
}
}, [darkTheme]);
return (
<div className="cursor-pointer">
<input
type="checkbox"
name="themeToggle"
id="checkbox"
className="hidden"
defaultChecked
onClick={() => {
setDarkTheme;
}}
/>
<label
htmlFor="checkbox"
className="text-yellow-500 md:hidden md:dark:block"
>
{/* <Sun /> */}
{darkTheme ? <Sun /> : <Moon />}
</label>
</div>
);
} This (arrow function equivalent) causes the invaild hook call error: const ThemeToggle: FunctionComponent = () => {
const [darkTheme, setDarkTheme] = useState(false);
useEffect(() => {
const body = document.querySelector('body');
if (darkTheme) {
body?.classList.add('dark');
} else {
body?.classList.remove('dark');
}
}, [darkTheme]);
return (
<div className="cursor-pointer">
<input
type="checkbox"
name="themeToggle"
id="checkbox"
className="hidden"
defaultChecked
onClick={() => {
setDarkTheme;
}}
/>
<label
htmlFor="checkbox"
className="text-yellow-500 md:hidden md:dark:block"
>
{darkTheme ? <Sun /> : <Moon />}
</label>
</div>
);
} What is really confusing for me is that the Astro docs use the above arrow function to achieve the functionality Also, this error occurs when I deploy to Vercel (site gets built) and seems to be caused by a dependency from which I use a react component. usage: {post.body.map((block: any) => (
(block._type == 'code')
?
<Code code={block.code} lang={block.language} theme={'github-dark'} />
:
<PortableText value={block} />
// toMarkdown(block)
))} the errors: ▶ src/pages/blog/[slug].astro15:14:28.048 | Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: This error occurs for each call to the |
I am also getting this same issue when I try to use the component library Chakra-UI. I've tried both the strict TS settings and JS settings. Both throw the same warnings and won't render correctly. Is there any ETA for a fix? |
same here, but export default function SomeComponent(){} seems to fixed it. is there any progress regarding what causing the issue? |
My console is once again at peace. Thank you. |
Yes thank you. Sigh. |
Maybe this should be reopened since the fix was reverted, or is it tracked somewhere else? |
I still face this issue when using arrow functions. I hope they'll fix this issue |
It doesn't look like there's a separate issue, or is fixed with a different PR. Re-opening. |
Experiencing this issue today
The following code logs the warning message: function Showcase() {
const [name, setName] = useState('Name here')
return <div>Hello { name }</div>
}
export default Showcase The following code does not log the warning message: export default function Showcase() {
const [name, setName] = useState('Name here')
return <div>Hello { name }</div>
} |
What version of
astro
are you using?1.0.0
Are you using an SSR adapter? If so, which one?
None
What package manager are you using?
yarn
What operating system are you using?
Mac
Describe the Bug
When running my site with the React plugin in development, I see
InvalidHookCall
warnings. In the repro, run:When you load http://localhost:3000/ you'll se the error in the terminal output.
The error only exists when using a hook, and given the simple usage, I'm confident the use is accurate. Thus, my assumption is that the cause is a mismatch in versions of React as cited in the third possibility of the message.
With my spelunking in #4084 I know that Astro uses a custom renderer, so I suspect the issues lies therein somewhere. As further evidence of my suspicion, in the official docs for this issue they recommend the following:
While this example obviously fails in SSR, if you do something simple like just logging "hello world" from that file, I see the log in the terminal (presumably for SSR) but not in the browser console, so I'm guessing react-dom is not used during hydration.
Link to Minimal Reproducible Example
https://github.com/cameronmcefee/invalid-hook-call-example
Participation
The text was updated successfully, but these errors were encountered: