Last active
November 22, 2021 13:34
-
-
Save nico-martin/4bd4bcbac096f250a07c230ec7a25a18 to your computer and use it in GitHub Desktop.
A React Context Provider to share State between reloads or even between tabs,
This file contains 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 characters
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') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment