Last active
November 22, 2019 09:49
-
-
Save jhonnymichel/9d138dcb6dd050212476876838eef952 to your computer and use it in GitHub Desktop.
hookstore-snippet-useStore
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 { store, useStore } from './hookstore'; | |
// setting the store initial state | |
store.state = 0; | |
function StatefulHello() { | |
// using the useStore hook | |
const [timesClicked, updateTimesClicked] = useStore(); | |
return ( | |
<div> | |
<h1>Hello, component!</h1> | |
<h2>The button inside this component was clicked {timesClicked} times</h2> | |
<button type="button" onClick={() => updateTimesClicked(timesClicked + 1)}> | |
Update | |
</button> | |
</div> | |
); | |
} | |
function AnotherComponent() { | |
// using the useStore hook here as well. The same state will be shared here. | |
const [ timesClicked ] = useStore(); | |
return ( | |
<div> | |
<h1> | |
Hello, this is a second component, with no relation to the one on the | |
top | |
</h1> | |
<h2> | |
Using the useStore hook, I know the user clicked on the button { timesClicked } times! | |
</h2> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment