首页 > 解决方案 > 关闭我的应用程序时,我得到处理程序不是函数,我正在使用 AppState

问题描述

我在 react-native 上使用 AppState,所以我可以知道我的应用程序何时处于活动状态或在后台。我可以使用 AppState 获得活动状态,但我无法获得后台状态,因为当我将应用程序加倍关闭它时,它会产生错误 Handler is not function。

我已经问到 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);

我除了我可以关闭应用程序并获取后台状态。仅供参考,我正在使用带有 backHandler 的 redux 导航,我不知道它是否会影响 AppState,但我知道这两个函数使用处理程序,

标签: react-native

解决方案


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


推荐阅读