Search code examples
react-nativetabbartabbarios

How can I get TabBarIOS to pop to the top of the navigation stack using react-native?


I have the following TabBarIOS.Item setup:

          <TabBarIOS.Item
            selected={this.state.selectedTab === 'tab1'}
            title='Tab 1'
            icon={require('./Components/Icons/IconImages/Tab1Icon.png')}
            onPress={() => {
              this.setState({
                selectedTab: 'tab1'
              });

             }}>
            <MyNavigatorIOS client={this.state.client} initialStep={this.state.initialStep} />

          </TabBarIOS.Item>

I am trying to use the onPress event to fire this.props.navigator.popToTop(); as per this example in the react native docs. The difference, however, is that I want the TabBarIOS onPress event to fire popToTop() and not the child MyNavigatorIOS component. How can I accomplish this?


Solution

  • I had the same issue, what you can do is add a ref to your view and to the navigator inside of it, like so:

              this.refs.timeline.refs.timelineNavigator.popToTop()
    

    So the TabBar code looks like this:

        <TabBarIOS.Item
          systemIcon="history"
          title="Timeline"
          badge={this.state.timelineBadge}
          selected={this.state.selectedTab === 'timeline'}
          onPress={() => {
            if (this.state.selectedTab === 'timeline') {
              this.refs.timeline.refs.timelineNavigator.popToTop()
            } else {
              this.setState({ selectedTab: 'timeline' })
            }
          }}>
          <Timeline
            ref="timeline"
          /> 
        </TabBarIOS.Item>