[英]React render expected expression
I am starting with React at the moment.我现在开始使用 React。 I followed the official guideline of reactjs.org on how to create a webpage that runs React.
我遵循 reactjs.org 关于如何创建运行 React 的网页的官方指南。 Then I just tried to modify the input I was renderung to a simple
<h1>
.然后我只是尝试将我渲染的输入修改为一个简单的
<h1>
。 However, as soon as I did this, I got unexpected expression error saying that "<" doesn't work.但是,一旦我这样做,我就收到了意外的表达式错误,说“<”不起作用。
Here's the code.这是代码。
const element = <h1>test</h1>;
const domContainer = document.querySelector('#like_button_container');
ReactDOM.render(element, domContainer);
I know this is really basic.我知道这是非常基本的。 Everywhere I looked it worked on their site, but I always get an error.
我在他们的网站上看到的任何地方都有效,但我总是遇到错误。
I used this to import React:我用它来导入 React:
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
This is what I wanted to do:这就是我想做的:
Thanks for your help :)谢谢你的帮助 :)
This because react expect a component to render, it can be a function or a class.这是因为 react 期望组件渲染,它可以是一个函数或一个类。
so you must wrap your element as a function or a class and name it with TitleCase.所以你必须把你的元素包装成一个函数或一个类,并用 TitleCase 命名它。
const Element = () => <div>element</div>
ReactDOM.render(<Element />, domContainer);
I think you should read more on they documentation Reactjs Documentation我认为你应该阅读更多关于他们的文档Reactjs 文档
I guess the fact that you're "importing" React means that you do not use webpack or any other building tool.我想您“导入” React的事实意味着您不使用webpack或任何其他构建工具。 So you do not transpile you code.
所以你不转译你的代码。 But
但
const element = <h1>test</h1>;
is not correct JS.是不正确的JS。 It's JSX .
这是JSX 。
Try to change this line to尝试将此行更改为
const element = React.createElement('h1', null, 'test');
See https://reactjs.org/docs/react-without-jsx.html见https://reactjs.org/docs/react-without-jsx.html
Or use some build toolchain.或者使用一些构建工具链。
You haven't configured JSX transpilation in your setup.您尚未在设置中配置 JSX 转译。 Check this link for using React without JSX: https://reactjs.org/docs/react-without-jsx.html .
检查此链接以在没有 JSX 的情况下使用 React: https ://reactjs.org/docs/react-without-jsx.html。
If you want to use JSX without a setup, add this script to your html: <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
and mark script tags with type="text/babel"
: https://reactjs.org/docs/add-react-to-a-website.html#quickly-try-jsx如果您想在没有设置的情况下使用 JSX,请将此脚本添加到您的 html:
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
并标记脚本带有type="text/babel"
标签: https : //reactjs.org/docs/add-react-to-a-website.html#quickly-try-jsx
Although you can add react in a script tag it's not really the recommended way as you need a setup for compiling jsx to js anyway in a real project.尽管您可以在脚本标签中添加反应,但它并不是真正推荐的方式,因为您需要在实际项目中设置将 jsx 编译为 js。
I recommend you use create-react-app for setting up a basic project.我建议您使用 create-react-app 来设置基本项目。
https://github.com/facebook/create-react-app https://github.com/facebook/create-react-app
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.