Skip to content

Instantly share code, notes, and snippets.

@hosseinmd
Last active June 30, 2020 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hosseinmd/2c1f37e68925fa6af26bf4b778f6e5e2 to your computer and use it in GitHub Desktop.
Save hosseinmd/2c1f37e68925fa6af26bf4b778f6e5e2 to your computer and use it in GitHub Desktop.
import React from "react";
import { createStore, createHook } from "react-global-hook";
const initialState = {
counter: 0
};
const actions = ({ setState, getState }) => ({
addToCounter(amount) {
const { counter } = getState()
setState({ counter: counter + amount });
},
});
const Store = createStore(initialState, actions);
const useStore = createHook(Store)
const App = () => {
const [state, actions] = useStore(["counter"]);
return (
<div>
<p>
counter:
{state.counter}
</p>
<button
type="button"
onClick={() => actions.addToCounter(1)}
>
+1 to counter
</button>
</div>
);
};
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment