useSession is breaking the first rule of hooks
See original GitHub issueDescribe the bug Breaking the first rule of hooks
Steps to reproduce
Expected behavior Do not break the first rule of hooks.
Screenshots or error logs https://github.com/nextauthjs/next-auth/blob/e6995d21cd1c7c0e701a305c91062ece83d48dfe/src/client/index.js#L145-L155
Additional context In the react docs it says in bold:
Don’t call Hooks inside loops, conditions, or nested functions
However, _useSessionHoo is clearly called conditionally. This could potentionally lead to issues.
https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Rules of Hooks - React
We're breaking the first rule by using a Hook in a condition if (name !== '') { useEffect(function persistForm() { localStorage.setItem('formData', name) ...
Read more >Invalid hook call. Hooks can only be called inside of the body ...
You are breaking the rules of hooks. Namely Don't call Hooks from regular JavaScript functions. You can only use a hook from synchronous ......
Read more >Breaking the Rules of React Hooks: Behind the Scenes
In this post, engineer Crystal Chang does a deep dive to uncover why it's important to follow the rules of React Hooks.
Read more >The First Rule of React Hooks, In Plain English - ITNEXT
The first rule of React Hooks is “only call hooks at the top level”. This means that within a given component, its hooks...
Read more >Understanding common frustrations with React Hooks
Some of the first custom React Hooks to hit the shelves were several ... Calling the useFetch Hook from an event handler breaks...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I read through your thread and I searched for an already existing issue for this, which I couldn’t find. So I openend up an issue. I do see some issues involving the
useSessionhook though.My suggestion would be to not break the first rule of hooks. I updated the comment to match your template.
I am pointing at a potential issue/bug (and it indeed is a bug) in your code here and you are closing it. If you don’t appreciate comments on your code - fair enough (even though you stated otherwise in your thread). But I generally find it pretty rude to close an issue that simply lacked of a bit formal templating syntax.
You probably have a rogue/duplicate
reactinstalled. Either globally, or throughnpm linkor similar.