-
-
Save sahilrajput03/cce8d6ec1e655f9029a0bf6352762f73 to your computer and use it in GitHub Desktop.
counter as hook #hooks #hook #counter #useCounter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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