Skip to content

Instantly share code, notes, and snippets.

@andregardi
Created September 12, 2020 13:01
Show Gist options
  • Save andregardi/17ca7d392408d651d4ad3f818fe6889b to your computer and use it in GitHub Desktop.
Save andregardi/17ca7d392408d651d4ad3f818fe6889b to your computer and use it in GitHub Desktop.
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