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.

useSession is breaking the first rule of hooks

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
mrtnbrodercommented, Sep 22, 2020

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 useSession hook though.

If you have a specific example of a problem / and or alternative implementation you would like to suggest please feel free to raise a bug report and/or a Pull Request.

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.

0reactions
balazsorban44commented, Jan 18, 2022

You probably have a rogue/duplicate react installed. Either globally, or through npm link or similar.

Read more comments on GitHub >

github_iconTop 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 >

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