-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Cannot use hooks in server components #52566
Comments
hooks are not allowed in server components as they run only once and don't run on the client (where hooks are supposed to be executed). If you want to use a hook, add If you still want to use a server component (if you want to fetch data directly on the server for ex), you can still pass props from a server component to a client component. More on the docs here : https://nextjs.org/docs/getting-started/react-essentials |
Thanks for the prompt reply, |
that's okay, technically you can't use a stateful hook ( If you HAVE TO name your function with |
I see, maybe we should mark this a feature request to be able to define hooks as "stateless" or something, so they are considered server component safe, |
I suppose the lint rule cannot distinguish between stateless and stateful hooks by only looking at the name |
Related PR on React’s side: facebook/react#27045 |
Is there a fix for that.... ? @acdlite |
Yeah i have the same issue my DB hook is called useDB which will pull the data from the DB which is a serverside api request which is allowed. Need some way to disable this. for these types of requests without disabling the rule. |
As the discussion has mentioned here, this is not a bug. As a workaround, you could disable the ESLint rule. But even better would be to change the naming of the function. If it's truly not a hook, then rather than P.S. we recently made our Server/Client Components more clear! |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.5.0: Mon Apr 24 20:52:24 PDT 2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T6000 Binaries: Node: 16.17.0 npm: 8.15.0 Yarn: 3.3.0 pnpm: N/A Relevant Packages: next: 13.4.10-canary.3 eslint-config-next: 13.4.10-canary.3 react: 18.2.0 react-dom: 18.2.0 typescript: 4.9.5 Next.js Config: output: N/A
Which area(s) of Next.js are affected? (leave empty if unsure)
App Router, ESLint (eslint-config-next)
Link to the code that reproduces this issue or a replay of the bug
https://github.com/wix/wix-appointments-subscriptions-nextjs-template
To Reproduce
Describe the Bug
Build fails with the error
React Hook "useMyHook" cannot be called in an async function. react-hooks/rules-of-hooks
Expected Behavior
Build should pass, hooks are allowed in server components provided that they don't use a client-only hook.
Which browser are you using? (if relevant)
node
How are you deploying your application? (if relevant)
Vercel/Netlify
NEXT-1437
The text was updated successfully, but these errors were encountered: