TypeError: (0, _native.useTheme) is not a function. (In '(0, _native.useTheme)()', '(0, _native.useTheme)' is undefined)

TypeError: (0, _native.useTheme) is not a function. (In '(0, _native.useTheme)()', '(0, _native.useTheme)' is undefined)

我创建了一个 drawerNavigator,现在我正在尝试向页眉添加一个图标。问题是当我添加 HeaderButton 时出现此错误:

Component exception

在 navigationOptions 中,我尝试同时使用 HeaderButton 和 CustomHeaderButton,但它不起作用,我似乎无法弄清楚问题所在。

这是我的代码:

HeaderButton.js

import React from "react";
import { HeaderButton } from "react-navigation-header-buttons";
import { Ionicons } from "@expo/vector-icons";

const CustomHeaderButton = (props) => {
  return (
    <HeaderButton
      {...props}
      IconComponent={Ionicons}
      iconSize={23}
      color="black"
    />
  );
};

export default CustomHeaderButton;

WelcomeScreen.js

import React from "react";
import { View, Text, StyleSheet, ImageBackground, Image } from "react-native";
import MainButton from "../components/MainButton";
import Colors from "../constants/Colors";
import { HeaderButtons, Item } from "react-navigation-header-buttons";
import HeaderButton from "../components/HeaderButton";

const WelcomeScreen = (props) => {
  return (
    <ImageBackground
      source={require("../assets/images/tsl.jpg")}
      style={styles.backgroundImage}
    >
      <Image
        source={require("../assets/images/slogan.jpg")}
        style={styles.logo}
      />
      <View style={styles.buttonContainer}>
        <MainButton
          onPress={() => {
            props.navigation.navigate({
              routeName: "UserLogin",
            });
          }}
        >
          User Login
        </MainButton>
        <MainButton
          onPress={() => {
            props.navigation.navigate({ routeName: "DriverLogin" });
          }}
        >
          Driver Login
        </MainButton>
        <View style={styles.newAccountContainer}>
          <Text style={styles.newAccountText}>Don't have an account?</Text>
        </View>
        <View style={styles.registerContainer}>
          <MainButton style={styles.registerButton}>Register now</MainButton>
        </View>
      </View>
    </ImageBackground>
  );
};

WelcomeScreen.navigationOptions = {
  headerLeft: (
    <HeaderButtons HeaderButtonComponent={HeaderButton}>
      <Item title="Menu" iconName="ios-menu" />
    </HeaderButtons>
  ),
};

谢谢!

您好,我遇到了完全相同的问题。我正在学习 Udemy 上的反应原生课程。

显然这是特定于课程代码等。但我希望这里的内容也能解决您的问题。

我尝试了一些方法,所以我不确定是什么解决了这个问题...这是我所做的:

  1. 我将 package.json 中的 react-navigation 和 react-navigation-header-buttons 版本更改为 Max 代码中使用的版本(分别为 3.11.1 和 3.0.1),我想我 运行 npm 安装。 (更新它们?)

应用程序无法运行,无法使用 react-navigation-header-buttons 启动错误

  1. 运行 expo update,更新包并重新构建 node_modules 和 package-lock.json

获得了未更新的软件包列表:expo-app-loading、react-navigation、react-navigation-drawer、react-navigation-header-buttons、react-navigation-stack、react-navigation-tabs

  1. 运行 expo install react-navigation react-navigation-drawer react-navigation-header-buttons react-navigation-stack react-navigation-tabs

  2. 注意到 react-navigation 警告 v3.13.0 不支持升级到 4.x

运行 npm install react-navigation@4 已安装 4.4.4

  1. react-navigation-header-buttons 仍然是旧版本 3.0.5,所以我 运行 npm install react-navigation-header-buttons@6 安装了 6.3.1

  2. npm start - 应用程序可在模拟器和物理设备上运行!

我的工作应用程序现在具有以下依赖项(在 package.json 中):

"expo": "~40.0.0",
"expo-app-loading": "^1.0.1",
"expo-status-bar": "~1.0.3",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-40.0.1.tar.gz",
"react-native-gesture-handler": "~1.8.0",
"react-native-reanimated": "~1.13.0",
"react-native-screens": "~2.15.2",
"react-native-web": "~0.13.12",
"react-navigation": "^4.4.4",
"react-navigation-drawer": "^2.7.0",
"react-navigation-header-buttons": "^6.3.1",
"react-navigation-stack": "^2.10.4",
"react-navigation-tabs": "^2.11.0"

我遇到了同样的问题,我做了 npm i --save @react-navigation/native,因为错误表明找不到 useTheme

您需要转到 package.json 文件,并将以下版本设置为“react-navigation-header-buttons”包:

"react-navigation-header-buttons": "^6.3.1"

然后 运行 再次 npm 安装并尝试 运行 您的应用程序。

致所有会出错的人(Max 的课程,vid 173 :)):

TypeError: (0, _native.useTheme) 不是函数。 (在'(0,_native.useTheme)()'中,'(0,_native.useTheme)'未定义)

尝试添加 npm i --save @react-navigation/native

如果没有帮助,请删除 node_modules & package-lock.json,检查并更新你的 package.json 到正确的版本。这是我的依赖项:

"dependencies": {
    "@expo/vector-icons": "^12.0.5",
    "@react-navigation/native": "^5.9.4",
    "expo": "40.0.0",
    "expo-app-loading": "^1.0.1",
    "expo-font": "~8.4.0",
    "expo-status-bar": "~1.0.3",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-40.0.1.tar.gz",
    "react-native-gesture-handler": "~1.8.0",
    "react-native-reanimated": "~1.13.0",
    "react-native-screens": "~2.15.2",
    "react-native-web": "^0.13.12",
    "react-navigation": "^4.4.4",
    "react-navigation-drawer": "^2.7.0",
    "react-navigation-header-buttons": "^6.3.1",
    "react-navigation-stack": "^2.10.4",
    "react-navigation-tabs": "^2.11.0",
    "react-redux": "^7.2.4",
    "redux": "^4.1.0",
    "redux-devtools-extension": "^2.13.9"

完成此操作后,只需 运行 npm install。

希望对大家有所帮助。学习和享受!=)

我也遇到过,解决方法是:

expo install react-navigation-header-buttons@6

因为npm install ...给我一个错误

检查 react-navigation-header-buttons 在 package.json 中的依赖性。它可能不是 版本 6。如果是这样,则从 package.json 中删除该行并使用 npm install --save react-navigation-header-buttons@6

重新安装版本 6 的 navigation-header-button

ionicons 列表中不再提供“md-cart”,请将 iconName 替换为有效名称,例如

iconName = "cart-outline"

在我的例子中,它将开始工作!

yarn add @react-navigation/native 或 npm i --save @react-navigation/native 应该可以解决问题。

对于所有会出错的人(在 Max 的课程中​​,vid 173:),运行 这个命令:

npm install react-navigation-header-buttons@6

它会解决这个问题。