在React Nativigation中如何更新状态goBack

时间:2017-07-19 06:00:37

标签: android react-native navigation react-navigation

上述问题说明了一切。我正在使用HomeScreen的StackNavigation。 HomesScreen具有位置组件和状态位置,其中位置存储在 AsyncStorage

使用 goBack()功能,我想导航到HomeScreen(导航部分已完成)和更新状态位置。我想更新位置状态以在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})
        });

流量:

enter image description here

如何将“goBack()”上的状态更新为HomeScreen?

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

1 个答案:

答案 0 :(得分:2)

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

在HomeScreen中:

   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()上传递值。