javascript - 警告 : React. createElement : type is invalid -- bundle. js

标签 javascript node.js reactjs webpack react-dom

我正在学习 React JS 的教程,一切都很好,几天来我可以运行一个示例,简单,执行推荐的基本配置,加上我添加的一些附加组件以识别 Javascript 版本.

经过几天不再审查项目,但它运行正常,执行命令时,我没有看到任何错误,但在浏览器中没有显示任何内容,仅在控制台中出现多个错误一个。

reac和react-dom卸载重装,问题依旧,尝试从 friend clone一个新项目,正常,只是复制了我的相同结构。

问题

Warning: React.createElement: 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.

The above error occurred in one of your React components: Consider adding an error boundary to your tree to customize error handling behavior.

需要注意的是,bundle.js文件出现错误,该文件用于存放通过webpack生成的代码

errores

tree

package.json

{
  "name": "prueba",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node server.js",
    "dev": "concurrently \"node server.js\" \"webpack -w\" "
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.2",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "serve-static": "^1.13.1"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "concurrently": "^3.5.1",
    "eslint": "^4.9.0",
    "eslint-config-airbnb-base": "^12.1.0",
    "eslint-plugin-import": "^2.7.0",
    "webpack": "^3.10.0"
  }
}

webpack.config.js

const path = require('path');

const config = {
    entry: './src/index.jsx',
    output: {
        path: path.resolve('js'),
        filename: 'bundle.js'
    },

    module: {
        rules: [
            {                
                test: /.jsx$/,
                use:{
                    loader:'babel-loader'
                },
                exclude: /node_module/
            }
        ]
    }
}

module.exports = config;

应用程序.jsx

import React, {Component} from 'react';
import {render} from 'react-dom';

class App extends Component{
    render(){
        return(
            <div>                
                <h1>Mi Aplicacion React Js</h1>
                <h3>Probando la exportacion</h3>
            </div>
        )       
    }
}

导出默认应用;

索引.jsx

import React, { Component } from 'react';
import { render } from 'react-dom';
import {App} from './components/app.jsx';

render(
    <App/>,
    document.getElementById('appStart')
)

index.html

<!DOCTYPE html>

<html>

    <head>
        <meta charset="utf-8">
        <title>Aprendiendo React</title>
    </head>

    <body>
        <div id="appStart"></div>
        <script src="js/bundle.js"></script>
    </body>

</html>

控制台

C:\Users\PterPmntaM\CursoReactJS\React_Scaffold> npm run dev

> prueba@1.0.0 dev C:\Users\PterPmntaM\CursoReactJS\React_Scaffold
> concurrently "node server.js" "webpack -w"

[0] Iniciando servidor
[1]
[1] Webpack is watching the files...
[1]
[1] Hash: 5fd2ce10b3c1788b385b
[1] Version: webpack 3.10.0
[1] Time: 4878ms
[1]     Asset    Size  Chunks                    Chunk Names
[1] bundle.js  729 kB       0  [emitted]  [big]  main
[1]   [14] ./src/index.jsx 381 bytes {0} [built]
[1]     + 27 hidden modules

最佳答案

在 app.jsx 中,组件是这样导出的:

export default App;

但是它是这样导入的:

import {App} from './components/app.jsx';

代码失败是因为 app.jsx 中不存在 App 导出,并且出现了 undefined 作为错误提示。它被导出为 default 而不是。

这是正确的导入方式:

// The recommended way
import App from './components/app.jsx';

// The alternative way, to better illustrate what's going on
import { default as App } from './components/app.jsx';

这是对 ES 模块的一个很好的概述:http://exploringjs.com/es6/ch_modules.html

关于javascript - 警告 : React. createElement : type is invalid -- bundle. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48738761/

相关文章:

javascript - 为什么 Javascript 比较不​​适用于对象?

javascript - 在 React JS 中重置状态

javascript - React 动画在组件卸载时不起作用

javascript - 将语义 ui 导入到 React 组件中

javascript - 将 API 派生值映射到 React.js 状态的问题

javascript - Firefox 扩展 : how to fill an input text field

javascript - 缓存并避免重新加载 html、css 和 js 文件

javascript - 如何构建类似 Gmail 的 Smart Compose?可能在文本区域?

javascript - 在发生事件时立即触发函数但等待 x 秒直到它能够再次运行?

node.js - Docker Compose 上的 Postgres ECONNREFUSED 与 NodeJS