next-redux-wrapper icon indicating copy to clipboard operation
next-redux-wrapper copied to clipboard

HYDRATE Action Dispatched twice with redux-toolkit

Open AlaaMX opened this issue 3 years ago • 6 comments

When using the redux toolkit and configuring store and importing slices the Hydrate action is dispatched twice for each page on SSR and SSG even when not using App.getInitialProps !

AlaaMX avatar Sep 25 '21 15:09 AlaaMX

Do you use react strict mode?

fostyfost avatar Sep 25 '21 23:09 fostyfost

Same, It's send duplicate data whenever I refresh f5 my page

igdev116 avatar Oct 05 '21 12:10 igdev116

Also faced this problem. Hydration occurs twice when using redux toolkit.

magicDvlp avatar Oct 11 '21 16:10 magicDvlp

Can anyone provide a reproducible scenario? Issues w/o codesandbox are not reviewed.

FYI https://github.com/kirill-konshin/next-redux-wrapper#app-and-getserversideprops-or-getstaticprops-at-page-level there are cases when HYDRATE can be dispatched twice.

kirill-konshin avatar Oct 13 '21 23:10 kirill-konshin

Seems it's because reactStrictMode: true in next.config.js. If reactStrictMode is enabled, withRedux(App)'s constructor is called twice. Therefore we got 2 HYDRATE actions.

I've made a codesandbox here. In the repo, toast is pushed only once in pages/index.js's getServerSideProps. But the console logs two toasts.

https://codesandbox.io/s/gifted-sun-mjun5

If I change reactStrictMode to false, the console only logs one toast.

However, it's only affected in development mode. See https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects

WanLinLin avatar Nov 08 '21 01:11 WanLinLin

@WanLinLin https://github.com/kirill-konshin/next-redux-wrapper/pull/419 check the new PR

kirill-konshin avatar Dec 03 '21 21:12 kirill-konshin

@WanLinLin, @kirill-konshin, I still experience the issue with the two consecutive hydration actions when reactStrictMode is enabled. Am I right, that such behavior is expected?

0x29a avatar Aug 19 '22 10:08 0x29a

Hi @0x29a, please disable reactStrictMode so you won't get two hydration actions.

WanLinLin avatar Aug 19 '22 13:08 WanLinLin

Hey @WanLinLin, thanks for the suggestion. Indeed, it works. However, doesn't this option exist to detect some undesired side effects during development?

0x29a avatar Aug 19 '22 14:08 0x29a

@WanLinLin is absolutely right, it is the behavior baked in Strict Mode, this library has nothing to do with it. Moreover, I should not override it anyhow. The entire tree has to be rendered twice, by design of Strict Mode.

kirill-konshin avatar Aug 19 '22 16:08 kirill-konshin