简体   繁体   English

setInterval 方法有什么问题?

[英]What's wrong with setInterval method?

I'm designing a screen in a mobile app that is supposed to disappear automatically and navigate to the next screen.我正在一个移动应用程序中设计一个屏幕,该屏幕应该自动消失并导航到下一个屏幕。 for that purpose I'm using setInterval in following code :为此,我在以下代码中使用 setInterval :

componentDidMount(){
      Animated.timing(this.state.AnimVal, {
          toValue: 1,
          duration: 500 ,
          delay: 1000,
          useNativeDriver: true,
      }).start();

      var timer = setInterval( () => {
        this.setState({
          isLoaded: true,
        });
      }, 1000);
      if(this.state.isLoaded){
        this.props.navigation.navigate({ routeName: 'Guideline'})
      };
  };

the isLoaded state variable method is supposed to become true and as a result of it, the screen must disappear. isLoaded 状态变量方法应该变为真,因此,屏幕必须消失。 but it does not happen and the screen remains.但它不会发生并且屏幕仍然存在。 any idea what's wrong with it?知道有什么问题吗?

this is because when the setInterval setState called, it will re-render the component, but they don not called componentDidMout.这是因为当 setInterval setState 调用时,它会重新渲染组件,但它们不会调用 componentDidMout。 在此处输入图片说明

for react-native > 0.6:对于本机 > 0.6:

static getDerivedStateFromProps(props, state){
if(state.isLoaded){
        //here may be has problem
        props.navigation.navigate({ routeName: 'Guideline'})
   };
}

for react-native < 0.6:对于反应原生 < 0.6:

componentWillReceiveProps(nextprops){
if(this.state.isLoaded){
        this.props.navigation.navigate({ routeName: 'Guideline'})
      };
}

I suggest it use componentDidUpdate for your condition.我建议它根据您的情况使用 componentDidUpdate 。 because getDerivedStateFromProps is static.因为 getDerivedStateFromProps 是静态的。 it can not call this.props.navigation它不能调用 this.props.navigation

componentDidUpdate(prevProps, prevState, snapshot) {
   if(this.state.isLoaded){
        this.props.navigation.navigate({ routeName: 'Guideline'})
      };
  }

The problem is , you are calling the问题是,您正在调用

if(this.state.isLoaded){
        this.props.navigation.navigate({ routeName: 'Guideline'})
      };

inside the componentDIdMount, so its always false, and even though setState re-renders it and makes the state of isLoadedTrue, but it doesnt call the componentDidMount as per the lifecyle methods of react.SO that condition never gets called again.在 componentDIdMount 内部,因此它始终为 false,即使 setState 重新渲染它并使状态为 isLoadedTrue,但它不会按照 react.SO 的生命周期方法调用 componentDidMount,因此该条件永远不会再次被调用。

You can try to write that particular code inside componentDidUpdate() .您可以尝试在 componentDidUpdate() 中编写该特定代码。

componentDidUpdate(){
if(this.state.isLoaded){
            this.props.navigation.navigate({ routeName: 'Guideline'})
          };
}

And one more thing, rather than setInterval try using setTimeout , coz that will suffice in your case.还有一件事,而不是setInterval尝试使用setTimeout ,因为这在您的情况下就足够了。 and use the setTimeout inside the componentDidMount itself.并在 componentDidMount 本身内部使用 setTimeout。 That logic of yours is fine, just the navigation logic put it into comopnentDidUpdate()你的那个逻辑很好,只是导航逻辑把它放到了comopnentDidUpdate()

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

相关问题 语法错误:以下代码有什么问题 - Syntax Error: What's wrong with the below code 我的Fire Base代码(React Native)出了什么问题 - What's wrong with my Fire Base code (React Native) 无法读取未定义的属性 'setHistory' 出了什么问题? - Cannot read property 'setHistory' of undefined What's wrong? 我的 FlatList 有什么问题? react-native - What's wrong with my FlatList? react-native 邮递员工作,但提取不行。怎么了? - Postman works but Fetch doesn't.what's wrong? 这个实现有什么问题? 我无法覆盖样式 - What's wrong with this implementation? I'm unable to override the style 在init()方法上引发错误的原因是什么? - What's the reason for the error being thrown on init() method? 我收到“不变违规”错误的渲染方法做错了什么? - What am I doing wrong with my render method that I'm receiving “Invariant Violation” error? React中的“无法读取未定义的属性&#39;map&#39;”,这是怎么回事? - “Cannot read property 'map' of undefined” within React, what's wrong here? Firebase 重置密码链接(未收到)使用 firebase react native 我的代码有什么问题? - Firebase Reset Password Link (Did not receive) using firebase react native what's wrong in my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM