Skip to content

Instantly share code, notes, and snippets.

@sahilrajput03
Forked from tannerlinsley/Counter.js
Last active October 22, 2020 10:51
Show Gist options
  • Save sahilrajput03/cce8d6ec1e655f9029a0bf6352762f73 to your computer and use it in GitHub Desktop.
Save sahilrajput03/cce8d6ec1e655f9029a0bf6352762f73 to your computer and use it in GitHub Desktop.
counter as hook #hooks #hook #counter #useCounter
const useCount = () => {
const [{ count }, setState] = useStore();
const increment = () => {
setState(old => ({
...old,
count: old.count + 1
}));
}
const decrement = () =>
setState(old => ({
...old,
count: old.count + 1
}));
}
return {count, increment, decrement}
}
const Counter = () => {
const { count, increment, decrement } = useCount()
return (
<div>
<button onClick={decrement}>-</button>
{count}
<button onClick={increment}>+</button>
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment