React Native Navigation const { navigate } = this.props.navigation;

N Sharma picture N Sharma · Mar 28, 2017 · Viewed 15.1k times · Source

I am learning react-native navigation https://reactnavigation.org/docs/intro/ . I see in examples there

class HomeScreen extends React.Component {
  static navigationOptions = {
    title: 'Welcome',
  };
  render() {
    const { navigate } = this.props.navigation;
    return (
      <View>
        <Text>Hello, Chat App!</Text>
        <Button
          onPress={() => navigate('Chat')}
          title="Chat with Lucy"
        />
      </View>
    );
  }
}

I could not understand what exactly this line of code is for const { navigate } = this.props.navigation;

Answer

Ahmed Eid picture Ahmed Eid · Mar 28, 2017

syntax has nothing to do with React Native it is called Destructuring assignment in es6 / es2015

const { navigate } = this.props.navigation;

is equivilent to with exception to var and const .

var navigate = this.props.navigation.navigate

the example without Destructuring should look like this

class HomeScreen extends React.Component {
  static navigationOptions = {
    title: 'Welcome',
  };
  render() {
    return (
      <View>
        <Text>Hello, Chat App!</Text>
        <Button
          onPress={() => this.props.navigation.navigate('Chat')}
          title="Chat with Lucy"
        />
      </View>
    );
  }
}