Skip to content

Instantly share code, notes, and snippets.

@andregardi
Created April 8, 2019 02:43
Show Gist options
  • Select an option

  • Save andregardi/fe1d836dc3b67e34e02fa3b461934cad to your computer and use it in GitHub Desktop.

Select an option

Save andregardi/fe1d836dc3b67e34e02fa3b461934cad to your computer and use it in GitHub Desktop.
import React from 'react';
import useGlobalHook 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 = useGlobalHook(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