setState in a function in component

I’ve done the binding in constructor, and the console.log’s post ok. But error = cannot read properties of undefined in the setState expression.
I’m getting same issue a lot trying variations and been struggling a bit.

link to full code (but was hoping someone knows issue without looking too deep):
https://codepen.io/camalDan/pen/ZEXdZNp
(will try not to edit for a while and have a break)

  timerCount() {
  console.log('tC',this.state.secCount)

    const id = setInterval(countDown(this.state.secCount), 1000);
    function countDown(secs) {
      if (secs <= 0) { return( clearInterval(id) ) }
      else {
        return(
          this.setState({
            secCount: secs-1,
          })
        )
      }
    }
  }
  
  render() {
      console.log('render',this.state.secCount)

    this.timerCount()

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36

Challenge: Build a 25 + 5 Clock

Link to the challenge:

plus: I’ve had a few infinite loops as a result of state not passing in properly (or whatever it is). Isn’t there some emergency ‘break’ button for stopping this? Or is it just too nooby to bother?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.