首页 > 解决方案 > 在 js-react native 中访问 props 和 navigation

问题描述

我正在尝试将道具和 {navigation, route} 放在同一个函数中,但它不起作用。我什至尝试声明一个变量而不是 prop,但这也不起作用。代码如下所示。某人可以告诉我我做错了什么。

function CustomDrawerContent(props, {navigation, route}) {
  //const { navigation } = props;
  const onSignOut = props.onSignOut ;
  return (
    <DrawerContentScrollView {...props}>
    <View>
    <Text> MENU </Text>
    </View>
      <DrawerItemList {...props} />
      <DrawerItem
        label="Logout"
        onPress={onSignOut}
      />
      <DrawerItem
        icon={(props) => <Ionicons name="ios-exit" {...props} />}
        label="close drawer"
        onPress={() => navigation.closeDrawer()}
      />
    </DrawerContentScrollView>
  );
}

标签: javascriptreact-props

解决方案


最好的方法是在as parameter of your component or to destructure the里面传递````props props``` 对象,但不要同时做这两个。你试过这样做吗?使用您需要的属性来解构 props 对象:

function CustomDrawerContent(props) {
  const { navigation, route, onSignOut } = props;

推荐阅读