当AppState处于非活动状态,后台或活动状态时,如何运行函数?

时间:2019-06-20 06:40:31

标签: react-native

这是我尝试过的。我不知道现在该怎么办。如果可以的话,我会很高兴

    componentDidMount(){
        AppState.addEventListener('change',() => this.handleAppStateChange)
    }

    componentWillUnmount(){
        AppState.removeEventListener('change',() => this.handleAppStateChange)
    }
    handleAppStateChange = (nextAppState) => {
        if(this.state.lastAppState === 'active' && nextAppState === 'inactive'){
            addIncome().then().catch(error => {})
        }
        this.setState({lastAppState: nextAppState})
    }

0 个答案:

没有答案