Skip to content

Instantly share code, notes, and snippets.

@dewey92
Last active May 17, 2019 18:59
Show Gist options
  • Save dewey92/c82ea11e1a27a01be7473e2dbfb1f62d to your computer and use it in GitHub Desktop.
Save dewey92/c82ea11e1a27a01be7473e2dbfb1f62d to your computer and use it in GitHub Desktop.
Counter Effect Cleaner
// 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