gpt4 book ai didi

javascript - 简单的 React.render() 给出错误

转载 作者:行者123 更新时间:2023-11-30 15:57:15 25 4
gpt4 key购买 nike

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

在我的 APP.js 文件中

var React = require('react');

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

module.exports = APP;

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)

最佳答案

您需要通过 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'));

关于javascript - 简单的 React.render() 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38319955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com