I'm creating interval counter and below code is working fine. But I have few questions about this code which I do not understand.
import React, { useState, useEffect } from 'react';
import {View, Text} from 'react-native'
const Interval = () => {
const [seconds, setSeconds] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
console.log(`seconds: ${seconds}`)
setSeconds(seconds => seconds + 1);
}, 1000);
return () => clearInterval(interval);
}, []);
return (
<View>
<Text>
{seconds} seconds have elapsed since mounting.
</Text>
</View>
);
};
export default IntervalExample;
- Why this is not working if I put
setSeconds(seconds => seconds + 1);insteadsetSeconds(seconds + 1);more simply ? - Why
console.log(`seconds: ${seconds}`)is always log as0?