Skip to content

Instantly share code, notes, and snippets.

@dewey92
Last active May 17, 2019 17:12
Show Gist options
  • Save dewey92/40a76aa1664176673532378fa79cc265 to your computer and use it in GitHub Desktop.
Save dewey92/40a76aa1664176673532378fa79cc265 to your computer and use it in GitHub Desktop.
Counter effects
// 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