Last active
May 17, 2019 17:12
-
-
Save dewey92/40a76aa1664176673532378fa79cc265 to your computer and use it in GitHub Desktop.
Counter effects
This file contains 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
// Counter.jsx | |
import * as React from 'react' | |
import { useCounterReducer } from './useCounterReducer' | |
const Counter = () => { | |
const [counter, dispatch] = useCounterReducer(); | |
React.useEffect(() => { | |
if (counter < 0) { | |
alert('Ya akhi, nilai counter ndak boleh di bawah 0 😚'); | |
dispatch('INCREMENT'); //biar bisa 0 lagi | |
} | |
}, [counter]); | |
return ( | |
<div> | |
<button onClick={() => dispatch('INCREMENT')}>+</button> | |
{counter} | |
<button onClick={() => dispatch('DECREMENT')}>-</button> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment