我刚刚创建了一个新的应用程序create-react-app
。
我用一个组件创建了一个新项目,并使用它webpack
来编译 JSXbabel
并将所有内容捆绑到/dist/bundle.js
.
我链接npm link
了应用程序和我在新项目中创建的组件。
在应用程序中我导入了组件,但我总是收到错误
元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。
打印应用程序中的组件它们都是undefined
.
这是组件:
index.js
import Root from './Root';
export { Root };
根.js
import React, { Component } from 'react';
class Root extends Component {
render() {
return (<div>{this.props.children}</div>);
}
}
export default Root;
webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
query: {
presets:['react']
}
}
}
]
}
};
包.json
{
"name": "my-comp",
"version": "0.0.1",
"description": "",
"main": "dist/bundle.js",
"dependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"react": "^16.2.0"
},
"devDependencies": {
"webpack": "^3.11.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --watch",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC"
}
在应用程序中
应用程序.js
import { Root } from 'my-comp';
render() {
return (
<Root>
<span>Hello World</span>
</Root>
);
}
有谁知道发生了什么?我不能从 导入bundle.js
吗?