next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

useSession's useEffect incompatible with React's strict mode

Open christoph-pflueger opened this issue 2 years ago • 3 comments

Environment

System: OS: Linux 5.10 Ubuntu 20.04.4 LTS (Focal Fossa) CPU: (12) x64 Intel(R) Core(TM) i7-5930K CPU @ 3.50GHz Memory: 9.57 GB / 15.58 GB Container: Yes Shell: 5.0.17 - /bin/bash Binaries: Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node Yarn: 1.22.15 - ~/.nvm/versions/node/v16.15.1/bin/yarn npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm npmPackages: next: 12.1.6 => 12.1.6 next-auth: ^4.6.1 => 4.6.1 react: 18.2.0 => 18.2.0

Reproduction URL

n/a

Describe the issue

As everybody knows, in React's strict mode, useEffects are run twice in development to reveal side effects. This causes useSession's useEffect to execute two requests instead of one.

How to reproduce

Should be self-explanatory.

Expected behavior

It should only perform one request, even in development using React's strict mode.

christoph-pflueger avatar Jun 23 '22 17:06 christoph-pflueger

This causes useSession's useEffect to execute two requests instead of one.

Can you describe the issue this is causing you?

balazsorban44 avatar Jun 23 '22 21:06 balazsorban44

This causes useSession's useEffect to execute two requests instead of one.

Can you describe the issue this is causing you?

I noticed it as I was debugging another issue and wasted some time until I realized that I didn't cause two requests. So nothing major at the moment.

But the reason useEffect runs twice since React 18 is to prepare for future features, that may depend on useEffect only running once. It could be ignored for now, but a simple flag w/ useRef doesn't sound like much of an effort - unless I'm missing something.

christoph-pflueger avatar Jun 23 '22 21:06 christoph-pflueger

Yeah, I guess we could do something like https://beta.reactjs.org/learn/synchronizing-with-effects#fetching-data

But this is more of an optimization than a bug, especially since the production build won't have this behavior.

Feel free to open a PR.

balazsorban44 avatar Jun 24 '22 09:06 balazsorban44

It looks like this issue did not receive any activity for 60 days. It will be closed in 7 days if no further activity occurs. If you think your issue is still relevant, commenting will keep it open. Thanks!

stale[bot] avatar Sep 08 '22 16:09 stale[bot]

To keep things tidy, we are closing this issue for now. If you think your issue is still relevant, leave a comment and we might reopen it. Thanks!

stale[bot] avatar Sep 17 '22 00:09 stale[bot]