react-native - 关闭我的应用程序时,我得到处理程序不是函数,我正在使用 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,但我知道这两个函数使用处理程序,
解决方案
我也碰到了这个。对我来说,这是因为我调用AppState.addEventListener
并AppState.removeEventListener
引用了一个不存在的处理程序。
推荐阅读
- ubuntu - 如果我的配置需要密码,如何将 openvpn 客户端作为守护进程运行
- angular - 角度管理界面
- javascript - 如何使用 window.location.hash 和 reactjs 获取 url 值?
- if-statement - 将 OR 添加到谷歌 if 和公式的中间?
- objective-c - 安装 cocoapods 时出错“sudo gem install cocoapods”MacOs Catalina
- javascript - 如何在 php 中回显 javascript 变量以将其以形式或锚点传递
- javascript - 如何正确加载和使用 Angular 9 中的 gapi.iframes 库?
- c# - 我应该如何将这些数据存储在数据库中?
- selenium - 单击表单提交按钮后,数据表未在 Selenium 中加载
- vb.net - 使用 VB.Net 更新 MS Acces 中的数据