bjoluc / next-redux-cookie-wrapper

Sync a subset of your Redux state with cookies in Next.js :cookie: :sparkles:
MIT License
114 stars 4 forks source link

Problem of hydration from the cookie data #50

Closed juliengueperoux closed 1 year ago

juliengueperoux commented 1 year ago

Hi,

I am facing an issue on a personal project. I am calling an API to have my data into getServerSideProps in the index.tsx. My goal is to save the data into a cookie so that I wouldn't have to call the API each time the user reload the page.

Thing is, I am all the time facing this error on page load : Error: Hydration failed because the initial UI does not match what was rendered on the server.

It is like the data retrieved from the api call was cleaned and deleted from the store after page load on the SSR... I mean, the hydration from the data received during SSR are not set in the CSR store

I am new to Nextjs and I would love to have your help :)

package.json

Capture d’écran 2023-03-17 à 09 19 39

store.ts

Capture d’écran 2023-03-17 à 09 22 29

index.tsx

Capture d’écran 2023-03-17 à 09 23 20

fishesSlice.ts

Capture d’écran 2023-03-17 à 09 21 18
bjoluc commented 1 year ago

Hi @juliengueperoux,

I'm glad to see you seem to have solved your problem.

My goal is to save the data into a cookie so that I wouldn't have to call the API each time the user reload the page.

This sounds like a perfect fit for getStaticProps: If you dispatch setFishes there, next-redux-wrapper will hydrate your client store with the fishes and the API will only be called at build time (or when regenerating) – no need to store fishes in a cookie then.