簡體   English   中英

反應錯誤:元素類型無效:期望使用字符串(對於內置組件)或類/函數(對於復合組件),但得到:對象

[英]React error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object

我從React得到以下錯誤,我的頁面無法加載: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

當我在React組件頂部使用import聲明時,例如import React from "react"

根據關於堆棧溢出的這個問題,答案是將module.exports = ComponentName更改為export default ComponentName ,但是在我的情況下,這兩段代碼都拋出相同的錯誤。

有人可以幫助我找出導致錯誤的原因嗎?

Register.jsx

import React from "react";

const Register = () => {
  return (
    <div>registration page</div>
  )
}
export default Register

webpack.js

const path = require('path');

module.exports = {
  mode: 'production',
  context: path.join(__dirname, './'),
  entry: './app/app.jsx',
  output: {
    path: path.join(__dirname, 'public'),
    filename: 'bundle.js',
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        // loader: 'jsx-loader',
        use: {
          loader: 'babel-loader',
        },
        exclude: /node_modules/,
        include: path.join(__dirname, 'app'),
      },
    ],
  },
};

.babelrc

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

您可能導入不正確。 由於您是默認導出,因此請從以下位置更改導入:

import { Register } from '<some_path>/Register';

至:

import Register from '<some_path>/Register';


或CommonJS樣式:

const Register = require('<some_path>/Register');

const Register = require('<some_path>/Register').default;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 反應:元素類型無效:應為字符串(對於內置組件)或類/函數(對於復合組件)但得到:對象 React-元素類型無效:需要一個字符串(對於內置組件)或一個類/函數(對於復合組件),但得到:對象 React - 錯誤:元素類型無效:需要一個字符串(對於內置組件)或一個類/函數(對於復合組件)但得到:未定義 反應錯誤:元素類型無效:應為字符串(對於內置組件)或類/函數(對於復合組件)但得到:未定義 錯誤:元素類型無效:預期為字符串(對於內置組件)或類/函數(對於復合組件)但得到:ReactJS 中的對象 未捕獲的錯誤:元素類型無效:期望一個字符串(對於內置組件)或類/函數(對於復合組件)但得到:對象 錯誤:元素類型無效:應為字符串(對於內置組件)或類/函數(對於復合組件)但得到:對象 錯誤:元素類型無效:應為字符串(對於內置組件)或類/函數(對於復合組件)但得到:對象 × 錯誤:元素類型無效:需要一個字符串(對於內置組件)或一個類/函數(對於復合組件)但得到:object React SSR 錯誤——元素類型無效:需要一個字符串(對於內置組件)或一個類/函數(對於復合組件)但得到:object
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM