G
G
Glglgl2022-01-28 19:07:25
React
Glglgl, 2022-01-28 19:07:25

Constant time tracking?

Good time of the day, please tell me how you can constantly track the time, I have half of the application tied to time, now I just have
const time = new Date (), but the problem is that the time is written to the constant at the entrance and is not updated further, you need keep an eye on him

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
chincharovpc, 2022-01-28
@chincharovpc

I think you should use setInterval

const [dateTime, setDateTime] = useState(new Date());
    useEffect(() => {
        const id = setInterval(() => setDateTime(new Date()), 1000);
        return () => {
            clearInterval(id);
        }
    }, []);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question