0
Devs, I created a clock that updates the state every second but I have the following question, someone could explain to me why setInterval works correctly only if it is inside the constructor?
class Clock extends Component {
constructor(props) {
super(props);
this.state = { date: new Date() }
// setInterval( () => {this.setState({ date: new Date() })}, 1000)
}
setInterval( () => { this.setState({ date: new Date() }) }, 1000);
render() {
return (
<View>
<Text style={{ fontSize: 42, textAlign: 'center' }}> {this.state.date.toLocaleTimeString()} </Text>
</View>
)
}
}/* CLOCK END*/