简体   繁体   English

React-Navigation:如何在TabNavigation中的选项卡之间传递数据?

[英]React-Navigation: How to pass data between tabs in TabNavigation?

I Have two tabs in tabNavigation 我在tabNavigation中有两个标签

const HomeStack = createStackNavigator({
    Home: HomeView
});

const SettingStack = createStackNavigator({
    Setting: SettingView
});

const TabNavigator = createBottomTabNavigator({
    Home: HomeStack,
    Setting: SettingStack
});

So I want to switch tab from HomeView to SettingView 所以我想将选项卡从HomeView切换到SettingView

// IN HOME VIEW //在主视图中

this.props.navigation.navigate('Setting', {
    someFlag: true,
    data: "SET"
});

via button action and want to send some data as below. 通过按钮操作,并希望发送如下数据。

// IN SETTING VIEW //在设置视图中

const { navigation } = this.props;
const openPCPSchedule = navigation.getParam("someFlag", false);
const data = navigation.getParam("data", null);
console.log("NAVI PARAMS: ", openPCPSchedule); // false
console.log("NAVI data: ", data); // null

Getting false and null at SettingView , Need a correct way to get data from one to tab to other? SettingView处得到falsenull ,需要一种正确的方法来将数据从一个选项卡获取到另一个?

We get props from previous tab using this.props.navigation. 我们使用this.props.navigation从上一个标签中获得道具。

While passing data add 在传递数据时添加

this.props.navigation.navigate('Setting', {
    someFlag: true,
    data: "SET"
});

To access above data on Setting screen add following code in componentDidMount method or in render method 要在“设置”屏幕上访问以上数据,请在componentDidMount方法或render方法中添加以下代码

this.props.navigation.state.params.someFlag // You can access someFlag as true here this.props.navigation.state.params.someFlag //您可以在此处以true的方式访问someFlag

You need to use dangerouslyGetParent() in SettingView. 您需要在SettingView中使用dangerouslyGetParent()地GetParent dangerouslyGetParent() this.props.navigation.navigate sends params to the parent, not to the screen. this.props.navigation.navigate将参数发送给父对象,而不是屏幕。 The code in SettingView will be: SettingView中的代码将是:

const { navigation } = this.props;
const openPCPSchedule = navigation.dangerouslyGetParent().getParam("someFlag", false);
const data = navigation.dangerouslyGetParent().getParam("data", null);

You can use screenProps 您可以使用screenProps

ScreenProps Usage Link Page ScreenProps使用情况链接页面

  • screenProps - Pass down extra options to child screens screenProps-将额外的选项传递给子屏幕

//MAIN VIEW //主视图

class YourApp extends Component {
  render() {
    const screenProps = {
         someFlag: true,
         data: "SET"
    }

    return (
      <TabNavigator screenProps={screenProps} />
    )
  }
}

export default YourApp

AppRegistry.registerComponent('YourApp', () => YourApp);

// IN SETTING VIEW //在设置视图中

class SettingScreen extends Component {
  render() {
    const { navigation, screenProps } = this.props

    return (
      <View>
        <Text>{screenProps.someFlag}</Text>
        <Text>{screenProps.data}</Text>
      </View>
    )
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM