‘setDate’ is not defined no-undef

  compiler-errors, javascript, reactjs

I’m trying to add a current time feature to my website and I ran into a problem with my code. When I run my code, it fails to compile, but when you inspect the page you can see the time being displayed. Here’s my code.

import React, { useState, useEffect } from 'react';

export const DateTime = () => {

var [date,setDate] = useState(new Date());

useEffect(() => {

    var timer = setInterval(()=>setDate(new Date()), 1000 )

    return function cleanup() {
        clearInterval(timer)
    }

});

return(
    <div>
        <p>Time : {date.toLocaleTimeString()}</p>
    </div>
)

}

export default DateTime;

Source: Ask Javascript Questions

LEAVE A COMMENT