react-native - addlistener focus react native 仅适用于第二次刷新 react-native
问题描述
因此,当我从第二个屏幕返回时,我一直在尝试从屏幕中的 asyncStorage 重新加载内容,但它只会在我来回导航时刷新
这是我的代码
componentDidMount() {
const {navigation} = this.props
navigation.addListener('focus', () => {
AsyncStorage.getItem('Servers').then((servers) => {
servers = JSON.parse(servers);
if (servers) {
return this.setState({servers:servers, loaded: true})
}
this.setState({servers: [], loaded: true});
});
});
};
另外,我认为每次完成 setState 时都应该重新渲染,但由于某种原因它没有这样做
解决方案
这是预期的行为...因为您只为焦点事件注册了一个侦听器...。直接在 componentDidMount 中执行 addListener 的回调...
componentDidMount() {
const {navigation} = this.props;
yourFocusHandler();
this.unsubscribe = navigation.addListener('focus', yourFocusHandler);
};
componentWillUnmount() {
this.unsubscribe();
}
推荐阅读
- json - 如何降低json中的级别?
- java - 即使用户没有点击 EditText 字段,也会调用 TextWatcher 的 afterTextChanged() 事件回调
- javascript - Angular 构建产品在运行时失败
- winforms - 写入日志以到达带有 serilog 扩展名的文本框
- reactjs - 当用户选择相同的选项时反应选择 onChange 触发器
- python - 您如何将 alpha vantage 日期设置为列?
- android - Use android dependency from Azure DevOps public artifact
- java - java - 如何使用secp256r1椭圆曲线密钥对在java中执行加密和解密?
- wso2 - WSO2 VFS FTP 代理动态创建
- amazon-web-services - 有没有办法在夜间触发 cloudwatch 警报时忽略它们?