Skip to content

Instantly share code, notes, and snippets.

@alonbardavid
Created October 2, 2019 12:26
Show Gist options
  • Save alonbardavid/5d28e1910175692bf5f344979601d9ff to your computer and use it in GitHub Desktop.
Save alonbardavid/5d28e1910175692bf5f344979601d9ff to your computer and use it in GitHub Desktop.
Patterns for deriving state gist4
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