Wix react-native-navigation change Tab and push screen

馋奶兔 提交于 2019-12-10 04:57:43

问题


How can I simultaneously switch tab and push screen? When the button is pressed, I would like to switch to another tab and push a new screen. Is it possible?

class Example extends Component {
      buttonHandler = () => {
        this.props.navigator.switchToTab({
          tabIndex: 0
        });
        this.props.navigator.push({  // actually this navigator's tabIndex is 0 
          screen: "dc.Examplecreen",
          title: "Exampe", 
          passProps: {
            lesson : this.props
          }    
       });
   }
}

回答1:


There is no any way to passProps with switchToTab. also i found in react-native-navigation but not get any solution. change tab and push using a global variable.

i am using react-native-navigation version 1.1.463

Change tab switchToTab

global.isComeFromBooking = true
this.props.navigator.switchToTab({
   tabIndex: 2,
});

in tab on index 2 setOnNavigatorEvent

componentDidMount() {
    this.props.navigator.setOnNavigatorEvent((e) => {
      if (e.id == 'willAppear' && global.isComeFromBooking) {
        this.props.navigator.push({
          screen: 'Reservations',
          title: 'Bookings',
          animated: true,
          style: {
            backgroundColor: 'black',
          },
          backButtonTitle: '',
        });
        global.isComeFromBooking = false
      }
    });
}



回答2:


You can switch the tab using the following code

Navigation.mergeOptions(this.props.componentId, {
  bottomTabs: {
    currentTabId: this.props.componentId
  }
});

and push using

Navigation.push(this.props.componentId, {
  component: {
    name: 'example.PushedScreen',
    passProps: {
      text: 'Pushed screen'
    },
    options: {
      topBar: {
        title: {
          text: 'Pushed screen title'
        }
      }
    }
  }
});

You can make a function combining these 2 codes. Not the best solution i know, but works!



来源:https://stackoverflow.com/questions/51871428/wix-react-native-navigation-change-tab-and-push-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!