Skip to content

Instantly share code, notes, and snippets.

@tannerlinsley
Last active February 4, 2019 14:58
Show Gist options
  • Save tannerlinsley/490272e89c9a822eb3819e97dd5759c1 to your computer and use it in GitHub Desktop.
Save tannerlinsley/490272e89c9a822eb3819e97dd5759c1 to your computer and use it in GitHub Desktop.
import { StoreProvider, useStore } from './store'
const Counter = () => {
// Use the store
const [state, setState] = useStore();
const increment = () =>
setState(old => ({
...old,
count: old.count + 1
}));
const decrement = () =>
setState(old => ({
...old,
count: old.count - 1
}));
return (
<div>
<button onClick={decrement}>-</button>
{state.count}
<button onClick={increment}>+</button>
</div>
);
};
const App = () => {
return (
// Provide the store to the app
<StoreProvider initialValue={{ count: 0 }}>
<Counter />
</StoreProvider>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment