2017-07-19 67 views
2

上面的问题说明了一切。我正在使用HomeScreen的StackNavigation。 HomesScreen具有组件位置和状态位置,其中位置存储到AsyncStorageIn React Nativigation如何更新状态goBack

使用GoBack的()功能,我想导航到主屏幕(导航部分已经完成),并更新状态位置。我想更新位置状态以在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}) 
     }); 

流量:

enter image description here

什么更新“goBack()”状态到HomeScreen?

有没有办法以更好的方式做到这一点?

+0

'this.setState({location:location,persistedLocation:location})'是一个异步,这可能是你的主屏幕没有获得更新值的原因 –

+0

为什么?即使我没有等待和异步也检查。你能告诉我是否可以在从位置设置器屏幕执行goBack()之后添加回调吗? – subhajit

回答

2

最后,经过大量的搜索,我终于在这里找到它:https://github.com/react-community/react-navigation/issues/288

在主画面:

this.props.navigate("Location", { onSelect: this.onSelect }); 

    onSelect = data => { 
    this.setState(data); 
    }; 

在位置屏幕:

const { navigation } = this.props; 
    navigation.goBack(); 
    navigation.state.params.onSelect({ selected: true }); 

妥善解决所有,用于改变先前的屏幕的状态下,上GoBack的传递值()。

相关问题