Last active
February 11, 2021 19:16
-
-
Save matheusml/a5e8f16ccf00ecc0e2ce9b09628a3f53 to your computer and use it in GitHub Desktop.
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
import React, { useState } from 'react'; | |
export function useCounter() { | |
const [count, setCount] = useState(0); | |
const decrement = () => setCount(count - 1); | |
const increment = () => setCount(count + 1); | |
return { | |
count, | |
decrement, | |
increment, | |
}; | |
} | |
export function Increment() { | |
const counter = useCounter(); | |
return ( | |
<Fragment> | |
<div>Count: {counter.count}</div> | |
<div> | |
<button onClick={counter.increment}>Increment</button> | |
</div> | |
</Fragment> | |
); | |
} | |
export function Decrement() { | |
const counter = useCounter(); | |
return ( | |
<Fragment> | |
<div>Count: {counter.count}</div> | |
<div> | |
<button onClick={counter.decrement}>Decrement</button> | |
</div> | |
</Fragment> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment