Created
March 21, 2021 21:21
-
-
Save florianmartens/ed01feba090abb595ea0070eb80d4860 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 { useState, useEffect, useRef } from "react"; | |
export default function App() { | |
const initalState = 0; | |
const [count, setCount] = useState(initalState); | |
const counterRef = useRef(initalState); | |
useEffect(() => { | |
counterRef.current = count; | |
}) | |
useEffect(() => { | |
setInterval(() => { | |
setCount(counterRef.current + 1); | |
}, 1000); | |
}, []); | |
return ( | |
<div className="App"> | |
<h1>The current count is:</h1> | |
<h2>{count}</h2> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment