react-native - React 抽屉导航 v5x useNavigation() 与组件类

标签 react-native

我曾多次尝试在组件类中使用 useNavigation() 进行导航。但我不能,因为根据错误,我应该在主体函数内部使用此方法。我在 render() 方法中尝试过。这也没有帮助。如果有人知道的话你能帮我吗?

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

export default class MenuDrawer extends React.Component{

render(){

const  navigation  = useNavigation();
return(
    <View>
       <Button  onPress={()=>{navigation.navigate('detail')}}  />
   </View>
);

}
}

最佳答案

根据文档here ,您可以将组件包装在函数中来使用它

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

export default yourFunction(props) {
  const navigation = useNavigation();

  return <MenuDrawer navigation={navigation} />;
}

您的新MenuDrawer

export default class MenuDrawer extends React.Component {
  render(){
    const { navigation } = this.props;
      return(
        <View>
         <Button onPress={()=>{navigation.navigate('detail')}}  />
       </View>
    );
  }
}

关于react-native - React 抽屉导航 v5x useNavigation() 与组件类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60274570/

相关文章:

java - React native - 如何修复 Java.lang Double 无法转换为 java.lang.Boolean

react-native - 无法解析“./GestureHandlerRootView

android - React Native 0.57.x <Image/> 大图像低质量

typescript - Firebase + Typescript,无法从用户对象中获取 accessToken?

javascript - ChatClient 中服务器关闭/重置后未触发错误事件

android - 如何为 react-native 应用程序设置 URL 方案/链接

react-native - Appium React Native 尚未准备好文本输入

themes - React Native Appearance.addChangeListener() 什么都不做

android - react-native 上的 Realm : can't get results when not debugging

javascript - 如何动态更改 DrawerNavigator 的导航选项(标题)?