Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save florianmartens/ed01feba090abb595ea0070eb80d4860 to your computer and use it in GitHub Desktop.
Save florianmartens/ed01feba090abb595ea0070eb80d4860 to your computer and use it in GitHub Desktop.
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