简体   繁体   English

反应渲染预期表达

[英]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.htmlhttps://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.

相关问题 无法渲染我的组件,需要赋值或 function 调用,而是在 React 中看到了一个表达式 - Can't render my component, Expected an assignment or function call and instead saw an expression in React 获得预期的赋值或函数调用,而是在尝试在 React 中呈现组件时看到表达式 no-unused-expressions 错误 - Getting Expected an assignment or function call and instead saw an expression no-unused-expressions error when trying to render a component in React 反应错误-渲染方法后预期的分号? - React error - expected semicolon after render method? 如何在React render()中正确设置条件表达式(if) - How to correctly setup a conditional expression (if) in a React render() 在 React JS 组件中使用 const 预期的表达式 - Expression expected with const at React JS component 使用Switch语句在React中期望表达 - Expression Expected in React using Switch Statement 关于React组件的ESLint规则:方法渲染预期没有返回值 - ESLint Rule on React Component: Method Render expected no return value return语句中react render()函数内部的意外令牌,预期为“,” - unexpected token, expected “,” inside of react render() function in the return statement React应用程序中渲染功能中的Javascript嗅探错误-预期为“}” - Javascript snipet error in render function in a react application - '}' expected array.map() 无法按预期渲染<li> React 中的组件</li> - array.map() does not work as expected to render <li> component in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM