ReactDOM 未捕获语法错误:意外标记 < [英] ReactDOM Uncaught SyntaxError: Unexpected token <

查看:38
本文介绍了ReactDOM 未捕获语法错误:意外标记 <的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉提出这样一个具体的问题,但我正在编写一个简单的教程,该教程使用以下 HTML 介绍 React

I'm sorry to ask such a specific question but I'm working on a simple tutorial which introduces React with the following HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Hello Separate</title>
</script>
  </head>
  <body>
    <h1>Hello Separate</h1>
    <div id="app"></div>
  </body>
</html>

以及使用 ReactDOM 在 div id ="app" 内创建

的脚本

And a script to create a <p> within the div id ="app" using ReactDOM

ReactDOM.render(
    <p>Rendered by React</p>,
    document.getElementById("app")
  )

我在这里提供了一个小提琴代码:

我不明白为什么我会收到错误 Uncaught SyntaxError: Unexpected token < 但认为它来自 ReactDOM.render 函数,任何人都可以提供洞察力?谢谢!

I don't understand why I'm getting the error Uncaught SyntaxError: Unexpected token < but think it's coming from the ReactDOM.render function, can anyone provide insight? Thank you!

推荐答案

你的代码有两个问题,

第一

您的脚本不正确.根据 docs,您应该添加这些脚本,>

Your scripts are not proper. As per docs, you should add these scripts,

<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>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

第二

可能是您在外部 JS 文件中编写 JS 代码.

Might be you are writing your JS code in external JS file.

来自文档,

现在你可以在任何 <script> 标签中使用 JSX,方法是向它添加 type="text/babel" 属性.

Now you can use JSX in any <script> tag by adding type="text/babel" attribute to it.

你只需要在你的 HTML 文件中添加这个 script

You need to add this script in your HTML file only,

<script type="text/babel">
  ReactDOM.render(
    <p>Rendered by React</p>,
    document.getElementById("app")
  )
</script>

演示

这篇关于ReactDOM 未捕获语法错误:意外标记 &lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆