Created
September 12, 2020 13:01
-
-
Save andregardi/17ca7d392408d651d4ad3f818fe6889b to your computer and use it in GitHub Desktop.
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 globalHook from 'use-global-hook'; | |
const initialState = { | |
counter: 0, | |
}; | |
const actions = { | |
addToCounter: (store, amount) => { | |
const newCounterValue = store.state.counter + amount; | |
store.setState({ counter: newCounterValue }); | |
}, | |
}; | |
const useGlobal = globalHook(React, initialState, actions); | |
const App = () => { | |
const [globalState, globalActions] = useGlobal(); | |
return ( | |
<div> | |
<p> | |
counter: | |
{globalState.counter} | |
</p> | |
<button type="button" onClick={() => globalActions.addToCounter(1)}> | |
+1 to global | |
</button> | |
</div> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment