2016-07-03 97 views
2

我有以下警告:警告:setState(...):只能更新已安装或已安装的组件。这通常意味着您在卸载的组件上调用了setState()。这是一个没有操作。请检查Home组件的代码。如何更改componentDidMount上React Native的状态?

export default class Home extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     loaded: false, 
     failed: false 
    }; 
    } 
    componentWillMount() { 
    Actions.auth(); 
    } 
    componentDidMount() { 
    Actions.loadUser.completed.listen(this.onLoadUserCompleted.bind(this)); 
    Actions.goHome.listen(this.onGoHome.bind(this)); 
    Actions.logout.listen(this.onLogout.bind(this)); 
    } 
    onLoadUserCompleted(user) { 
    let currentUser = DataStore.getCurrentUser(); 
    this.setState ({ loaded: true }); // <============= 
    } 
} 

回答

0

componentDidMount()可以用的setState发生状态转换。我们的代码中有很多这些东西,请参阅下面的示例。我想setState()的另一个调用会导致此警告,也许在另一个视图/组件? (或者您的组件出于某种原因意外卸载)

constructor(props) { 
    super(props); 

    this.state = { 
     runAfterInteractions: false, 
    }; 
    } 

    componentDidMount() { 
    InteractionManager.runAfterInteractions(() => { 
     this.setState({ 
     runAfterInteractions: true, 
     }); 
    }); 
    } 

    render() { 

    if(!this.state.runAfterInteractions) return null; 
    .... 
相关问题