Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何避免在componentWillMount中设置状态_Reactjs_React Native_Setstate - Fatal编程技术网

Reactjs 如何避免在componentWillMount中设置状态

Reactjs 如何避免在componentWillMount中设置状态,reactjs,react-native,setstate,Reactjs,React Native,Setstate,我有一个欢迎屏幕,我只想向用户显示一次。如果我使用异步存储来保存令牌,它就会起作用。它改变了ShowWelcomeSlides的状态,所以我可以设置一个条件。但是我有一个错误消息。如果不是在componentWillMount中,我真的不知道在哪里可以更新状态。有什么想法吗 警告:只能更新已安装或正在安装的组件。这通常意味着您在未安装的组件上调用setState、replaceState或forceUpdate。这是禁止的 componentWillMount() { this._r

我有一个欢迎屏幕,我只想向用户显示一次。如果我使用异步存储来保存令牌,它就会起作用。它改变了ShowWelcomeSlides的状态,所以我可以设置一个条件。但是我有一个错误消息。如果不是在componentWillMount中,我真的不知道在哪里可以更新状态。有什么想法吗

警告:只能更新已安装或正在安装的组件。这通常意味着您在未安装的组件上调用setState、replaceState或forceUpdate。这是禁止的

  componentWillMount() {
    this._renderWelcomeSlidesWithAsyncStorage().done();
  }

  _renderWelcomeSlidesWithAsyncStorage = async () => {
    let value = await AsyncStorage.getItem('AlreadyVisited');

            if (value==='true'){
              this.setState({ShowWelcomeSlides: 'false'});
              console.log('token is present')
            }else{
              this.setState({ShowWelcomeSlides: 'true'});
              console.log('token is not present');
              this._setValue();
            }
        }; 

      onSlidesComplete = () => {
        this.props.navigation.navigate('Home');
      }

  render(){

    if (this.state.ShowWelcomeSlides==='true'){
        return(
        <Slides data={SLIDE_DATA} onComplete={this.onSlidesComplete} />
      );
    }return(
     <SkipWelcome />
    );
componentWillMount(){
此._renderwelcomesslideswithasyncstorage().done();
}
_renderwelcomesslideswithasyncstorage=async()=>{
let value=wait AsyncStorage.getItem('AlreadyVisited');
如果(值=='true'){
this.setState({ShowWelcomeSlides:'false'});
console.log('令牌存在')
}否则{
this.setState({ShowWelcomeSlides:'true'});
log('令牌不存在');
这是。_setValue();
}
}; 
onSlidesComplete=()=>{
this.props.navigation.navigate('Home');
}
render(){
if(this.state.ShowWelcomeSlides==='true'){
返回(
);
}返回(
);

我相信警告是针对componentWillMount的使用。您应该在componentDidMount中调用_renderWelcomeSlidesWithAsyncStorage以确保您的应用程序已安装。

如果是AsyncOK就可以了,所以我可以继续使用该应用程序,它不会导致问题?根据我的经验,当您的异步方法完成时会出现此警告组件正在卸载。这可能是一个很难追踪的情况,因为从UI的角度来看,似乎一切都正常。我会检查您的流程,确保组件没有在某个地方被装载和卸载。对于componentDidMount,警告仍然存在。您认为这会是App Store的问题吗?Ar你确定吗?可能你正在安装应用程序之前设置应用程序的状态。代码段是否完整?这是怎么回事?_setValue做什么?可能在卸载组件后调用它。
_setValue=async()=>{await AsyncStorage.setItem('AlreadyVisited','true');};
如果令牌不存在,它会存储令牌