score:4

Accepted answer

your code does not render anything related to the current time. this line:

<p>{setinterval(this.displaytime,1000)}</p>

does not print the current time - it displays created interval's id, because that's what is returned by setinterval() function.

so, first of all, you should change this line to display the time, based on component's state. this can be done like this:

<p>{this.state.time}</p>

another thing you have to do is to properly create the interval. setting it in render() method is not a good idea, because you will create a new interval

componentdidmount() {
    // arrow function allows you to use "this" in context of the component
    this.interval = setinterval(() => {
        this.displaytime();
    }), 1000);
}

(you should also remember to deactivate the interval once component is removed from the view).


Related Query

More Query from same tag