【问题标题】:In React Nativigation how to update the state goBack在 React Navigation 中如何更新状态 goBack
【发布时间】: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


【解决方案1】:

经过大量搜索,我终于在这里找到了它: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() 上传递值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-01
    • 2021-11-17
    • 2018-05-28
    • 1970-01-01
    • 2018-09-02
    • 2023-03-05
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多