Something went wrong. Try again.
Starship Simulator Controls thoriumsim.com
Something went wrong. Try again.
891 B · 31 lines
TSX
at develop
1234567891011121314151617181920212223242526272829303132import { useReducer, useEffect, useCallback, type Reducer } from "react";
export function useSessionStorageReducer<R extends Reducer<I, any>, I>( reducer: R, defaultState: I, storageKey: string,) { const init = useCallback(() => { let preloadedState: any; try { preloadedState = JSON.parse(window.sessionStorage.getItem(storageKey) || ""); // validate preloadedState if necessary } catch { // ignore } return preloadedState || defaultState; }, [storageKey, defaultState]);
const hookVars = useReducer(reducer, null, init);
const hookyHook = hookVars[0]; useEffect(() => { sessionStorage.setItem(storageKey, JSON.stringify(hookyHook)); }, [storageKey, hookyHook]);
return hookVars;}
export function useSessionStorage<T>(storageKey: string, defaultValue: T) { return useSessionStorageReducer((state: T, action: T) => action, defaultValue, storageKey);}