const useCounter = () => {
const [count, setCount] = React.useState(0)
React.useEffect(() => {
const interval = setInterval(() => {
setCount(previous => previous + 1)
}, 1000)
return () => {
clearInterval(interval)
}
}, [])
return count
}
function App () {
const count = useCounter()
return (
<div>{count}</div>
)
}