Я не могу использовать this.props.navigation.navigate в версии 5 - PullRequest
1 голос
/ 24 апреля 2020
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;

Я пытаюсь вызвать в своем заголовке, используя this.props.navigation.openDrawer ()

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

Возвращает следующую ошибку: TypeError: undefined не является объектом (оценивает _this.props .navigation.openDrawer () ')

1 Ответ

1 голос
/ 24 апреля 2020

Поскольку вы используете v5, вы можете использовать DrawerActions из @act-navigation / native

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

И сделать это так:

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

Надеюсь, это поможет!

...