You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How to properly synchronize the localStorage state between the client and the server?
I use authorization with a Bearer token.
Naturally, I get an error of desynchronization, hydration of the state on the server and on the client.
I wrote this code.
"use client";import{FC,PropsWithChildren,useEffect,useState}from"react";import{Loader}from"@gravity-ui/uikit";import{useAuthStore}from"../../store/auth";exportconstStorageLoader: FC<PropsWithChildren>=({ children })=>{const[isHydrated,setIsHydrated]=useState(false);useEffect(()=>{constcheckAuthHydration=()=>{constauthHydrated=useAuthStore.persist.hasHydrated();if(authHydrated){setIsHydrated(true);}};constauthUnsubHydrate=useAuthStore.persist.onHydrate(()=>setIsHydrated(false),);constauthUnsubFinishHydration=useAuthStore.persist.onFinishHydration(()=>{checkAuthHydration();},);checkAuthHydration();return()=>{authUnsubHydrate();authUnsubFinishHydration();};},[]);if(!isHydrated){return<Loadersize="l"/>;}returnchildren;};
Maybe someone has an example of how to cook it properly? I'm still not sure if I did it right.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
How to properly synchronize the localStorage state between the client and the server?
I use authorization with a Bearer token.
Naturally, I get an error of desynchronization, hydration of the state on the server and on the client.
I wrote this code.
Maybe someone has an example of how to cook it properly? I'm still not sure if I did it right.
Beta Was this translation helpful? Give feedback.
All reactions