javascript - React-Navigation: How to pass data between tabs in TabNavigation? - Stack Overflow

I Have two tabs in tabNavigationconst HomeStack = createStackNavigator({Home: HomeView});const Settin

I Have two tabs in 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

// 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?

I Have two tabs in 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

// 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?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Aug 13, 2019 at 8:59 Abhishek ThapliyalAbhishek Thapliyal 3,7186 gold badges35 silver badges72 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You need to use dangerouslyGetParent() in SettingView. this.props.navigation.navigate sends params to the parent, not to the screen. The code in SettingView will be:

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

We get props from previous tab using 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 ponentDidMount method or in render method

this.props.navigation.state.params.someFlag // You can access someFlag as true here

You can use screenProps

ScreenProps Usage Link Page

  • screenProps - Pass down extra options to child screens

//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>
    )
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745280656a4620261.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信