Created
October 2, 2019 12:26
-
-
Save alonbardavid/5d28e1910175692bf5f344979601d9ff to your computer and use it in GitHub Desktop.
Patterns for deriving state gist4
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
function ticker(){ | |
const [renderTimes,rerender] = useState(0); | |
const now = new Date().getTime(); | |
const [start] = useState(now); | |
//we use this to force the function to rerender every second | |
useEffect(()=>{ | |
const intervalId = setInterval(()=>rerender(renderTimes + 1),1000) | |
return ()=>clearInterval(intervalId) | |
}) | |
return <div> | |
seconds elapsed {Math.floor((now - start)/1000} | |
</div> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment