Skip to main content

getFromLocalStorage

Avoid error if window has not loaded

––– views

Avoid error if window has not loaded

Code

export function getFromLocalStorage(key) { if (typeof localStorage !== 'undefined') { return localStorage.getItem(key); } return null; }
javascript

Usage

const [token, setToken] = useState(() => getFromLocalStorage('token') || null);
jsx