react import 问题
我代码如下: index.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Header from './header';
import './index.scss';
class App extends Component {
render() {
return (<Header></Header>);
}
}
const app = window.document.getElementById('app');
ReactDOM.render(<App />, app)
;
header.js
```js
import React, { Component } from 'react';
export default class Header extends Component {
render() {
return (<section className="rheader">
fuck
</section>);
}
}
两个文件中我必须显式import React而且R必须大写,写成小写都不行。否则就报Uncaught ReferenceError: React is not defined。 这是什么原因,是必须这样做还是和babel的配置有关系?我babelrc文件如下
{ "presets": ["es2015", "stage-0","react"],
"plugins": [
"add-module-exports",
"transform-decorators-legacy",
"transform-class-properties",
"transform-runtime"]
}
3 回复
或者是webpack的配置有关?
找到个解决方法 用
new webpack.ProvidePlugin({
React: 'react',
}),
因为react.js里export出来的变量名就叫React