简体   繁体   English

在 React Native App 中,我得到了错误:元素类型无效:期望一个字符串(对于内置组件)...检查 `App` 的渲染方法?

[英]In React Native App, I Got the Error: Element type is invalid: expected a string (for built-in components) ... Check the render method of `App`?

today while working on react native project I was trying to import SearchBar from react-native-paper package but this was giving me this weird error which I'm not able to resolve:今天在处理本机项目时,我试图从 react-native-paper package 导入 SearchBar 但这给了我这个我无法解决的奇怪错误:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up 
default and named imports.

Check the render method of `App`.

here is my app.js :这是我的 app.js

import React from "react";
import { StatusBar as ExpoStatusBar } from "expo-status-bar";
import { StyleSheet, Text, View, SafeAreaView, StatusBar } from "react-native";
import { SearchBar } from "react-native-paper";

export default function App() {
  return (
    <>
      <SafeAreaView style={styles.container}>
        <View style={styles.search}>
          <Text>Search</Text>
          <SearchBar />
        </View>
        <View style={styles.list}>
          <Text style={styles.text}>List</Text>
        </View>
      </SafeAreaView>
      <ExpoStatusBar style="auto" />
    </>
  );
}

const styles = StyleSheet.create({
  container: {
    marginTop: StatusBar.currentHeight,
    flex: 1,
    alignItems: "center",
    justifyContent: "center",
  },

  search: {
    padding: 16,
    backgroundColor: "green",
  },

  list: {
    flex: 1,
    padding: 16,
    backgroundColor: "blue",
  },

  text: {
    color: "white",
  },
});

error says that issue is with import/export, but I don't see any issue with that, please help.错误说问题与导入/导出有关,但我没有看到任何问题,请帮忙。

complete error looks like this:完整的错误如下所示:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up 
default and named imports.

Check the render method of `App`.

This error is located at:
    in RCTView (created by View)
    in View (created by App)
    in RCTView (created by View)
    in View
    in SafeAreaView (created by App)
    in App (created by ExpoRoot)
    in ExpoRoot
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
at node_modules\react-native\Libraries\LogBox\LogBox.js:149:8 in registerError
at node_modules\react-native\Libraries\LogBox\LogBox.js:60:8 in errorImpl
at node_modules\react-native\Libraries\LogBox\LogBox.js:34:4 in console.error
at node_modules\expo\build\environment\react-native-logs.fx.js:27:4 in error
at node_modules\react-native\Libraries\Core\ExceptionsManager.js:104:6 in reportException
at node_modules\react-native\Libraries\Core\ExceptionsManager.js:172:19 in handleException
at node_modules\react-native\Libraries\Core\ReactFiberErrorDialog.js:43:2 in showErrorDialog
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:15792:34 in logCapturedError   
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:15884:20 in update.callback    
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:7199:2 in callCallback
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:7220:20 in commitUpdateQueue   
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:16632:25 in commitLifeCycles   
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:19216:22 in commitLayoutEffectsat node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:93:4 in invokeGuardedCallbackProd
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:318:2 in invokeGuardedCallback 
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18952:29 in commitRootImpl     
at node_modules\scheduler\cjs\scheduler.development.js:468:23 in unstable_runWithPriority
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18791:17 in commitRoot
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18192:12 in performSyncWorkOnRoot
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5911:33 in runWithPriority$argument_1
at node_modules\scheduler\cjs\scheduler.development.js:468:23 in unstable_runWithPriority
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5906:23 in flushSyncCallbackQueueImpl
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5893:28 in flushSyncCallbackQueue
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18244:28 in flushSync
at node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:20353:13 in scheduleRefresh    
at node_modules\react-refresh\cjs\react-refresh-runtime.development.js:218:8 in mountedRoots.forEach$argument_0
at node_modules\react-refresh\cjs\react-refresh-runtime.development.js:210:4 in performReactRefresh
at node_modules\react-native\Libraries\Core\setUpReactRefresh.js:43:6 in Refresh.performReactRefresh
at node_modules\metro-runtime\src\polyfills\require.js:655:10 in setTimeout$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:123:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:379:16 in callTimers
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:414:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:113:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:365:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:4 in callFunctionReturnFlushedQueue

Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `App`.
    in App (created by ExpoRoot)
    in ExpoRoot
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer

and much more....以及更多....

My package.json is:我的 package.json 是:

{
  "name": "mealstogo",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "lint": "eslint . --ext .js"
  },
  "dependencies": {
    "@babel/eslint-parser": "^7.16.5",
    "eslint-plugin-flowtype": "^8.0.3",
    "expo": "~44.0.0",
    "expo-status-bar": "~1.2.0",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-paper": "^4.11.2",
    "react-native-web": "0.17.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@react-native-community/eslint-config": "^3.0.1",
    "eslint": "^8.7.0",
    "prettier": "^2.5.1"
  },
  "private": true
}

You are trying to destruct the import, but it seems like you don't have to do it.您正在尝试破坏导入,但似乎您不必这样做。

If you take a look at the source code of SearchBar on Github , at the bottom of the file you can see export default <...>如果您查看GithubSearchBar的源代码,在文件底部您可以看到export default <...>

It means that代表着

import { SearchBar } from "react-native-paper";

may become可能会变成

import SearchBar from "react-native-paper";

See this other Stack Overflow issue for more detail.有关更多详细信息,请参阅其他Stack Overflow 问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 元素类型无效:期望一个字符串(对于内置组件)...检查`App`的render方法 - Element type is invalid: expected a string (for built-in components) … Check the render method of `App` 渲染错误元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - Render Error Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined React Native,元素类型无效:需要一个字符串(对于内置组件) - React Native, element type is invalid: expected a string (for built-in components) React - 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - React - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 反应错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - React Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 错误:元素类型无效:应为字符串(用于内置组件)]但得到:未定义。 Nextjs,反应 - Error: Element type is invalid: expected a string (for built-in components) ]but got: undefined. Nextjs, React Expo React Native 错误:元素类型无效:应为字符串(对于内置组件) - Expo React Native Error: Element type is invalid: Expected a string (for built-in components) 元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件),但得到:object。 React-Native - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. React-Native 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义的 React - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined React 错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。 反应 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM