Last active
November 22, 2021 13:34
-
-
Save nico-martin/4bd4bcbac096f250a07c230ec7a25a18 to your computer and use it in GitHub Desktop.
Revisions
-
nico-martin revised this gist
Nov 22, 2021 . 2 changed files with 34 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ import React from 'react'; import ReactDOM from 'react-dom'; import { SharedStorageProvider, useSharedStorage, } from './sharedStateContext.tsx'; const App = () => { const [sharedState, setSharedState] = useSharedStorage(); return ( <div> <p>Current Count: {sharedState.count}</p> <button onClick={() => setSharedState({ count: sharedState.count + 1, }) } > increase </button> </div> ); }; ReactDOM.render( <SharedStorageProvider> <App /> </SharedStorageProvider>, document.querySelector('#app') ); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ const Context = React.createContext<{ setState: () => {}, }); export const SharedStorageProvider = ({ children, storeKey = 'test', useSessionStorage = false, @@ -50,7 +50,7 @@ export const LocalStorageProvider = ({ ); }; export const useSharedStorage = (): [StateI, (state: StateI) => void] => { const { state, setState } = React.useContext(Context); return [state, setState]; }; -
nico-martin created this gist
Nov 22, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ import React from 'react'; interface StateI { [key: string]: any; } const Context = React.createContext<{ state: StateI; setState: (state: StateI) => void; }>({ state: {}, setState: () => {}, }); export const LocalStorageProvider = ({ children, storeKey = 'test', useSessionStorage = false, }: { children: any; storeKey?: string; useSessionStorage?: boolean; }) => { const [store, setStore] = React.useState<StateI>({}); const storage = useSessionStorage ? sessionStorage : localStorage; React.useEffect(() => { setStore(getStore()); window.addEventListener('storage', (event) => { if (event.key === storeKey) { setStore(getStore()); } }); }, []); const getStore = (): StateI => JSON.parse(storage.getItem(storeKey)) as StateI; const updateStore = (part: StateI) => { const store = getStore(); const newStore = { ...store, ...part }; storage.setItem(storeKey, JSON.stringify(newStore)); setStore(newStore); }; return ( <Context.Provider value={{ state: store, setState: updateStore }}> {children} </Context.Provider> ); }; export const useLocalStorage = (): [StateI, (state: StateI) => void] => { const { state, setState } = React.useContext(Context); return [state, setState]; };