简体   繁体   English

在 componentDidMount 中调用函数反应原生

[英]calling a function in componentDidMount react native

I am trying to call a function upon mounting a component, this function controls some state manipulations.我试图在安装组件时调用一个函数,这个函数控制一些状态操作。 I am not sure I am doing it right.我不确定我做得对。 I want the timer to restart once it gets to 20, since there is no button, I am assuming it should be done in the componentDidMount.我希望计时器在达到 20 时重新启动,因为没有按钮,我假设它应该在 componentDidMount 中完成。 can someone please point me in the right direction.有人可以指出我正确的方向吗? below is a trimmed down sample code of what I am trying to achieve.下面是我试图实现的精简示例代码。

        constructor(props) {
          super(props);
          this.state = { 
            timer: 10,
            timesup: false,
            timing: true,
            showWelcome: true, 
            };
        }

        componentDidMount() {
          this.endTimer();
        this.clockCall = setInterval(() => {
          this.decrementClock();
        }, 1000);
        }

        endTimer = () => {
          if (this.state.timer <= 25) {
                this.setState({
                  timing: true,
                  timer: 30,
                  showWelcome: true,
                  timesup: false,
                })
          }

        }


        decrementClock = () => {  
          this.setState((prevstate) => ({ 
            timer: prevstate.timer-1 
          }), () => {
            if(this.state.timer === 0) {
              clearInterval(this.clockCall)
              this.setState({
                timesup: true,
                timing: false,
                showWelcome: false,
              })
            }
          })
        }


        componentWillUnmount() {
        clearInterval(this.clockCall);
        }


        render() {
          return (
            <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>

        {this.state.timesup && (
            <Text style={{fontSize: 18, color: '#000'}}>
            Time up
            </Text>)}


        {this.state.timing && (
            <Text style={{fontSize: 18, color: '#000'}}>
            {this.state.timer}
            </Text>)}

              {this.state.showWelcome && (
                <Text style={{ fontSize: 20 }}>Welcome</Text>
              )}
            </View> 
          )
        }
        }

I want the timer to restart once it gets to 20, since there is no button, I am assuming it should be done in the componentDidMount .我希望计时器在达到 20 时重新启动,因为没有按钮,我假设它应该在componentDidMount 中完成。

No, you need to use componentDidUpdate lifecycle method to check timer 's current value.不,您需要使用componentDidUpdate生命周期方法来检查timer的当前值。 componentDidMount is called only once at the mounting stage. componentDidMount在安装阶段只调用一次。

So, remove this.endTimer();所以,删除this.endTimer(); from componentDidMount .来自componentDidMount

  componentDidMount() {
    this.clockCall = setInterval(() => {
      this.decrementClock();
    }, 1000);
  }

Then implement componentDidUpdate method like this:然后像这样实现componentDidUpdate方法:

  componentDidUpdate(){
    if(this.state.timer <= 20){
      this.endTimer();
    }
  }

endTimer() like this: endTimer()像这样:

  endTimer = () => {
      this.setState({
        timing: true,
        timer: 30,
        showWelcome: true,
        timesup: false,
      })
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM