简单 React.render() 给出错误

Simple React.render() gives error

我正在尝试创建一个简单的 React 应用程序,它会呈现一些内容。安装了 react 和 webpack 所需的依赖项,甚至更多的包,但是 none 有效

在我的 APP.js 文件中

var React = require('react');

var APP = React.createClass({
render: function(){
    return(<h1>Hello React</h1>);
}
});

module.exports = APP;

应用程序-client.js文件

var React = require('react');
var APP = require('./components/APP');

React.render(<APP />, document.getElementById('react-container'));

在 运行 命令 webpack 它给出错误:

ERROR in ./app-client.js
Module build failed: SyntaxError: /Users/akash/Dropbox/learning/pollingApp/start/app-client.js: Unexpected token (4:13)
2 | var APP = require('./components/APP');
3 | 
> 4 | React.render(<APP />, document.getElementById('react-container'));
    |              ^
at Parser.pp.raise (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/location.js:22:13)
at Parser.pp.unexpected (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/util.js:89:8)
at Parser.pp.parseExprAtom (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:522:12)
at Parser.pp.parseExprSubscripts (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:277:19)
at Parser.pp.parseMaybeUnary (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:257:19)
at Parser.pp.parseExprOps (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:188:19)
at Parser.pp.parseMaybeConditional (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:165:19)
at Parser.pp.parseMaybeAssign (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:128:19)
at Parser.pp.parseExprListItem (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:1046:16)
at Parser.pp.parseCallExpressionArguments (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:353:20)
at Parser.pp.parseSubscripts (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:316:31)
at Parser.pp.parseExprSubscripts (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:287:15)
at Parser.pp.parseMaybeUnary (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:257:19)
at Parser.pp.parseExprOps (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:188:19)
at Parser.pp.parseMaybeConditional (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:165:19)
at Parser.pp.parseMaybeAssign (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:128:19)
at Parser.pp.parseExpression (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/expression.js:92:19)
at Parser.pp.parseStatement (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/statement.js:163:19)
at Parser.pp.parseBlockBody (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/statement.js:529:21)
at Parser.pp.parseTopLevel (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/statement.js:36:8)
at Parser.parse (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/parser/index.js:129:19)
at parse (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babylon/lib/index.js:47:47)
at File.parse (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babel-core/lib/transformation/file/index.js:517:34)
at File.parseCode (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babel-core/lib/transformation/file/index.js:603:20)
at /Users/akash/Dropbox/learning/pollingApp/start/node_modules/babel-core/lib/transformation/pipeline.js:49:12
at File.wrap (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babel-core/lib/transformation/file/index.js:563:16)
at Pipeline.transform (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babel-core/lib/transformation/pipeline.js:47:17)
at transpile (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babel-loader/index.js:14:22)
at Object.module.exports (/Users/akash/Dropbox/learning/pollingApp/start/node_modules/babel-loader/index.js:88:12)

您需要使用以下语法:https://facebook.github.io/react/blog/2015/10/01/react-render-and-top-level-api.html

ReactDOM.render(reactElement, domContainerNode)

你需要通过 react-dom 模块来使用 React dom API。 ReactDom.render 现在是用于渲染 React 组件的新 API

var ReactDom = require('react-dom');
var APP = require('./components/APP');

ReactDom.render(<APP />, document.getElementById('react-container'));