Skip to content
Advertisement

How to display working digital clock using React

I want to render digital clock on the browser , i don’t know where to use setInterval() function in my script and also the what will be the name of function used as a first argument.

JavaScript

Advertisement

Answer

For this you’re going to need a few things:

  1. a setInterval to update the time
  2. a variable in the component’s state for keeping track of the time
  3. a safe way of adding and removing the setInterval when the component mounts and unmounts respectively

This component will do all of those things:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement