Ruan Duarte
Ruan Duarte

Reputation: 385

I can't use this.props.navigation.navigate in version 5

import 'react-native-gesture-handler';
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createDrawerNavigator } from '@react-navigation/drawer';

import News from './src/screens/News';
import Photos from './src/screens/Photos'

const Drawer = createDrawerNavigator();

function App() {
  return (
    <NavigationContainer>
      <Drawer.Navigator initialRouteName="News" >
        <Drawer.Screen name="News" component={News}/>
        <Drawer.Screen name="Photos" component={Photos} />
      </Drawer.Navigator>
    </NavigationContainer>
  );
}

export default App;

I try to call in my header using this.props.navigation.openDrawer()

<View style={style.iconHeader}>
  <TouchableOpacity onPress={() => this.props.navigation.openDrawer()}>
    <Feather name='menu' size={36} color='black' />
  </TouchableOpacity>
</View>

Returns the following error: TypeError: undefined is not an object (evaluating '_this.props.navigation.openDrawer()')

Upvotes: 1

Views: 457

Answers (1)

Goskula Jayachandra
Goskula Jayachandra

Reputation: 4201

As you are using v5 you can use DrawerActions from @react-navigation/native

import { useNavigation, DrawerActions } from '@react-navigation/native';

And do it like this:

    const navigation = useNavigation();
      return (
      <View style={style.iconHeader}>
      <TouchableOpacity   onPress={() => {
          navigation.dispatch(DrawerActions.openDrawer());
        }}>
        <Feather name='menu' size={36} color='black' />
      </TouchableOpacity>
    </View>

Hope this helps!

Upvotes: 1

Related Questions