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
const [isSidebarExpanded, setIsSidebarExpanded] = useState(() => {
if (typeof window !== 'undefined') {
const saved = window.localStorage.getItem('sidebarExpanded');
if (saved === null) {
return true;
}
return JSON.parse(saved);
}
return true; // default state if window is not defined
});
useEffect(() => {
if (typeof window !== 'undefined') {
window.localStorage.setItem(
'sidebarExpanded',
JSON.stringify(isSidebarExpanded),
);
}
}, [isSidebarExpanded]);
const toggleSidebar = () => {
setIsSidebarExpanded(!isSidebarExpanded);
};
Error: Hydration failed because the initial UI does not match what was rendered on the server.
This particular part brings Hydration Error. So for that I used 2 useEffects instead of directly initializing via function and created a mounted state to check if component is mounted or not. Then track the state changes.
Want to know if there is something else that can be done and is efficient.
This particular part brings Hydration Error. So for that I used 2 useEffects instead of directly initializing via function and created a mounted state to check if component is mounted or not. Then track the state changes.
Want to know if there is something else that can be done and is efficient.
my updates:
Here I am using isCollapsed instead of isExpanded.
The text was updated successfully, but these errors were encountered: