Trying to follow a simple clock/countdown tutorial in React:
constructor(props) {
super(props);
this.state = {
secondsRemaining: 10
};
}
componentDidMount(){
let interval = setInterval(this.timer, 1000);
this.setState({ secondsRemaining: this.state.secondsRemaining })
this.setState({ interval: interval });
};
componentWillUnmount() {
clearInterval(this.state.interval);
};
timer(){
this.setState({ secondsRemaining: this.state.secondsRemaining -1 });
};
Very obvious what everything does but when I run it, I get an error saying cannot read property secondsRemaining of undefined
in the timer function
It is probably something stupid I have missed out but I just cannot see it
followed the answer to this question: setInterval in a React app