React-native-tab-view: How to use navigation state in tab child views ?

Created on 20 Feb 2018  路  4Comments  路  Source: satya164/react-native-tab-view

Expected behaviour

Navigation state should be accessible

Actual behavior

it throws error -> undefined is not a function (evaluation this.state.navigate('mynewpage'))

Code sample, screenshots

Parent:
style={styles.container}
navigationState={this.state}
renderScene={this._renderScene}
renderHeader={this._renderHeader}
onIndexChange={this._handleIndexChange}
initialLayout={initialLayout}
/>

Child: mynewpage
this.state.navigate('mynewpage') it throws error -> undefined is not a function (evaluation this.state.navigate('mynewpage'))

What have you tried

I have setted the navigation state in the Tab view page (Parent).But while accessing the navigation state in my child views render method using this.state.navigate('mynewpage') it throws error -> undefined is not a function (evaluation this.state.navigate('mynewpage'))
style={styles.container}
navigationState={this.state}
renderScene={this._renderScene}
renderHeader={this._renderHeader}
onIndexChange={this._handleIndexChange}
initialLayout={initialLayout}
/>

Most helpful comment

    this.state = {
      isOpen: false,
      routes: [
        {
          key: 'View1',
          icon: 'ios-paper',
          color: '#fff',
          navigation: this.props.navigation // pass navigation
        },
        {
          key: 'View12',
          icon: 'add',
          color: '#fff',
        }
      ],
    };

Usage in View1
this.props.route.navigation.navigate('AnotherScene')

All 4 comments

To access parent's state in a child, you need to pass it down as a prop, like any other React component.

Also, the library has no such API as this.state.navigate. I don't know what you're referring to.

    this.state = {
      isOpen: false,
      routes: [
        {
          key: 'View1',
          icon: 'ios-paper',
          color: '#fff',
          navigation: this.props.navigation // pass navigation
        },
        {
          key: 'View12',
          icon: 'add',
          color: '#fff',
        }
      ],
    };

Usage in View1
this.props.route.navigation.navigate('AnotherScene')

Thank's in view i have use :
const { navigate } = this.props.route.navigation;
and
onPress={() => navigate('Articles', { id: 86 })}

Work fine ! 馃拑

`   this.state = {
      search: '',
      index: 0,
      routes: [
        { key: 'first', title: 'Mes Mandats', navigation: this.props.navigation },
        { key: 'second', title: 'Partag茅s', navigation: this.props.navigation },
        { key: 'third', title: 'Re莽us', navigation: this.props.navigation },
      ],
    };
  }`
`const renderScene = ({ route }) => {
  switch (route.key) {
    case 'first':
      return <MandatesList navigation={route.navigation} />;
    case 'second':
      return <SecondRoute />;
    default:
      return null;
  }
};`

Thanks it works for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

itzsaga picture itzsaga  路  3Comments

AndriiUhryn picture AndriiUhryn  路  3Comments

glennvgastel picture glennvgastel  路  3Comments

f6m6 picture f6m6  路  3Comments

hyochan35 picture hyochan35  路  3Comments