关闭我的应用程序时,我得到的处理程序不是函数,我正在使用AppState

时间:2019-04-11 02:30:33

标签: react-native

我在react-native上使用AppState,因此我可以知道我的应用程序何时处于活动状态或在后台。我可以使用AppState获得活动状态,但无法获得背景状态,因为当我再次回退应用程序以将其关闭时,它将产生错误处理程序不起作用。

我已经问过github了,但是答案不起作用,他们关闭了线程,所以很烦https://github.com/facebook/react-native/issues/24383

这是我的代码:

class SplashScreen extends Component {

    constructor(props){
        super(props)
        this.state = ({
            appState : AppState.currentState,
        })
    }

    fetchingAll = async (state) => {
        if(state == 'active'){
            await this.props.getEquipmentTier();
            await this.props.getEquipmentCraft();
            await this.props.getMapData();
            await this.props.getMonsterLocation();
            await this.props.getMonsterElement();
            await this.props.getMonsterDrop();
            await this.props.getItemData();
            await this.props.getCardData();
            await this.props.getEquipmentData();
            await this.props.getMonsterdata();
        }
        return true;
    }


    componentWillMount() {
        AppState.addEventListener('change', this.fetchingAll(this.state.appState));
    }


//If i am include this (componentDidUnMount) on my code the MainMenu will show loading becuse this.props.navigation.addListener('DidFocus', () => {
)
not execute.

/*componentWillUnMount() {
        AppState.removeEventListener('change', this.fetchingAll(this.state.appState));
    }*/

    componentDidUpdate(){
        if (!this.props.isLoading) {
            this.props.navigation.dispatch(resetAction);
        }
    }

    render() {
        return (
            <ImageBackground
                source={require('../style/images/launch_screen.png')}
                style={{flex: 1}}
                resizeMode='stretch'
            >
                <View style={style.imageFullScreen}>
                    <StatusBar hidden/>
                    <ActivityIndicator
                        size="large"
                        color="#00796b"
                    />
                    <Text style={style.indicatorText}>Please wait ...</Text>
                </View>
            </ImageBackground>
        );
    }
}

const mapStateToProps = (state) => {
    return {
        isLoading: state.MonsterReducer.isLoading,
    }
}

const mapDispatchToProps = (dispatch) => {
    return {
        getMonsterdata: () => dispatch(fetchingMonsterFromAPI()),
        getEquipmentData: () => dispatch(fetchingEquipmentFromAPI()),
        getCardData: () => dispatch(fetchingCardFromAPI()),
        getItemData: () => dispatch(fetchingItemFromAPI()),
        getMonsterDrop: () => dispatch(fetchingMonsterDropFromAPI()),
        getMonsterElement: () => dispatch(fetchingMonsterElementFromAPI()),
        getMapData: () => dispatch(fetchingMapFromAPI()),
        getMonsterLocation: () => dispatch(fetchingMonsterLocationFromAPI()),
        getEquipmentCraft: () => dispatch(fetchingEquipmentCraftFromAPI()),
        getEquipmentTier: () => dispatch(fetchingEquipmentTierFromAPI()),
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(SplashScreen);

我除了可以关闭应用程序并获取背景状态外。 仅供参考,我正在将redux导航与backHandler一起使用,我不知道它是否对AppState起作用,但是我知道那两个使用handler的函数,

1 个答案:

答案 0 :(得分:1)

我也碰到了这个。对我来说,是因为我曾经叫AppState.addEventListenerAppState.removeEventListener并引用了一个不存在的处理程序。