Last active
May 17, 2019 18:59
-
-
Save dewey92/c82ea11e1a27a01be7473e2dbfb1f62d to your computer and use it in GitHub Desktop.
Counter Effect Cleaner
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, effect], actions] = useCounterReducer(); | |
// Hooks ini hanya berfokus pada "side-effect" dari reducer di atas | |
React.useEffect(() => { | |
if (effect === 'INVALID_STATE') { | |
alert('Ya akhi, nilai counter ndak boleh di bawah 0 😚'); | |
} | |
// reset effect menjadi `undefined` kembali setelah menampilkan alert | |
return action.resetEffect; | |
}, [effect]); | |
return ( | |
<div> | |
<button onClick={actions.increment}>+</button> | |
{counter} | |
<button onClick={actions.decrement}>-</button> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment