I'm trying to create persistent state with localStorage and React hooks in Next.js SSR app and everything seems to look good but when I reloade page after update my data I'm getting error:
Warning: Text content did not match. Server: "0" Client: "5"
What can I do to repair that? This is my code:
import React, { createContext, useContext, useReducer } from 'react';
import Reducer from '../utils/Reducer';
const StoreContext = createContext();
let defaultState = {
count: 0,
message: "",
pageView: 0,
};
export const StoreProvider = ({ children }) => {
const [state, dispatch] = useReducer(Reducer, {}, () => {
const localData = process.browser ? localStorage.getItem('state') : null
return localData ? JSON.parse(localData) : defaultState
});
return (
<StoreContext.Provider value={{state, dispatch}}>
{children}
</StoreContext.Provider>
)
}
export const useStore = () => useContext(StoreContext);