Created
October 26, 2018 23:12
-
-
Save andrepcg/8aac734765714c85ff19a2a58e05cfd8 to your computer and use it in GitHub Desktop.
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
import React, { useState } from "react"; | |
const useCounter = (initialValue) => { | |
const [count, setCount] = useState(initialValue); | |
const increment = () => setCount(count + 1); | |
const decrement = () => setCount(count - 1); | |
return { count, increment, decrement }; | |
}; | |
function Counter() { | |
const { count, increment, decrement } = useCounter(0); | |
return ( | |
<div> | |
<p>{count}</p> | |
<button onClick={increment}> | |
increment | |
</button> | |
<button onClick={decrement}> | |
decrement | |
</button> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment