2016-07-03 99 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; 
    .... 
相關問題