【问题标题】:react-native and react-navigation: Accesing params from sibling screen in TabNavigatorreact-native 和 react-navigation:从 TabNavigator 中的同级屏幕访问参数
【发布时间】:2019-07-30 22:52:32
【问题描述】:

我正在从同一 TabNavigator 中的另一个屏幕访问 TabNavigator 中的一个屏幕中的参数。

这就是我目前的做法:

const siblingNavigator = props.navigation.dangerouslyGetParent().getChildNavigation('Cities');
const data = siblingNavigator.getParam('data');

这看起来很复杂,而且可能有问题。

有没有更好的办法?

【问题讨论】:

    标签: javascript react-native react-navigation


    【解决方案1】:

    将所需数据传递给screenProps。不知道我现在用的这个功能是什么bug,但是我在使用的时候没看到过。

    const SomeStack = createBottomTabNavigator({
      // config
    });
    <SomeStack
      screenProps={/* this prop will get passed to the screen components as this.props.screenProps */}
    />
    

    示例

    const SomeStack = createBottomTabNavigator({
      // config
    });
    ...
    class MainScreen extends React.Component {
      constructor(props) {
        super(props);
        this.state = {data: "somedata"};
      }
    
      render() {
        return(<SomeStack screenProps={{ data: this.state.data}} />
      }
    }
    

    // 现在您可以在屏幕上进行操作了

    console.log(this.props.screenProps.data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 2020-10-17
      相关资源
      最近更新 更多