React-native-tab-view: Jump to tab programmatically

Created on 11 Jul 2017  路  5Comments  路  Source: satya164/react-native-tab-view

Hi! I am trying to make a tab change when onPress an inside route component button and I can't manage to pass props and change the state of the index.

HomeScreen (Child) Component:
<Button title="Add new" onPress={() => { pass index number to Parent Component Where TabViewAnimated is }>

Main (Parent) Component:
_renderScene = SceneMap({ '1': AddNew, // Trying to get to here '2': HomeScreen, });

I've been searching all over google and can't manage to pass props and change the state ..

Most helpful comment

have a call back in your Main Component and pass that to your HomeScreen via props

In Main

_updateRoute(newIdx) {
this.setState({index: newIdx})
}

pass the handler together in the TabViewAnimated:

      <TabViewAnimated
        style={styles.container}
        navigationState={this.state}
        renderScene={this._renderScene}
        renderHeader={this._renderHeader}
        onRequestChangeTab={this._handleChangeTab}
        changeRoute={this._updateRoute.bind(this)}
      />

In HomeScreen
<Button title="Add new" onPress={this.props.changeRoute(<route number>)}>

All 5 comments

have a call back in your Main Component and pass that to your HomeScreen via props

In Main

_updateRoute(newIdx) {
this.setState({index: newIdx})
}

pass the handler together in the TabViewAnimated:

      <TabViewAnimated
        style={styles.container}
        navigationState={this.state}
        renderScene={this._renderScene}
        renderHeader={this._renderHeader}
        onRequestChangeTab={this._handleChangeTab}
        changeRoute={this._updateRoute.bind(this)}
      />

In HomeScreen
<Button title="Add new" onPress={this.props.changeRoute(<route number>)}>

@ixje Thank you!!

This doesn't work for the version 0.0.69. I can't find a way to do it.

@zhukovka probably because of the breaking changes. Try renaming the onRequestChangeTab prop to onIndexChange.

SceneMap
Helper function which takes an object with the mapping of route.key to React components and returns a function to use with renderScene prop.

renderScene = SceneMap({
first: FirstRoute,
second: SecondRoute,
});
Each scene receives the following props:

route: the current route rendered by the component
jumpTo: method to jump to other tabs, takes a route.key as it's argument

so just make a prop "jumpTo(key)" in your route components and use it inside of them

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahmedrowaihi picture ahmedrowaihi  路  3Comments

ios-dev-newbie picture ios-dev-newbie  路  3Comments

itzsaga picture itzsaga  路  3Comments

satya164 picture satya164  路  3Comments

glennvgastel picture glennvgastel  路  3Comments