Avoid error if window has not loaded
Code
getFromLocalStorage.tsx
export function getFromLocalStorage(key) {
if (typeof localStorage !== 'undefined') {
return localStorage.getItem(key);
}
return null;
}
Usage
const [token, setToken] = useState(() => getFromLocalStorage('token') || null);