【发布时间】:2017-07-19 06:00:36
【问题描述】:
上面的问题说明了一切。我有一个 HomeScreen 的 StackNavigation 。 HomesScreen 有一个组件 Location 和一个状态 location ,其中位置存储到 AsyncStorage。
使用 goBack() 函数,我想导航到 HomeScreen(导航部分已完成)并 更新 状态位置。我想更新位置状态以在 HomeScreen 中呈现位置。
主屏幕渲染:
<View>
<Text style=>{(this.state.location) ? (this.state.location) : ('Select Current Location')}</Text>
</View>
位置组件
await AsyncStorage.getItem('location').then((location) => {
this.setState({location: location, persistedLocation: location})
});
await AsyncStorage.getItem('region').then((region) => {
this.setState({region: region, persistedRegion: region})
});
流程:
如何将“goBack()”上的状态更新到 HomeScreen ?
有没有更好的方法?
【问题讨论】:
-
this.setState({location: location, persistedLocation: location})是异步的,这可能是您没有在主屏幕中获得更新值的原因 -
为什么?即使我也检查过没有等待和异步。你能告诉我是否可以在从位置设置器屏幕执行 goBack() 后添加回调吗?
标签: android react-native navigation react-navigation