You need to enable JavaScript to run this app. React.js代码在Live Server运行异常 未输出Hello World
优惠活动
大模型
产品
解决方案
定价
更多

React.js代码在Live Server运行异常 未输出Hello World

无法渲染Hello World的原因与修正方案

根本错误

代码存在严格的大小写拼写错误,直接导致运行中断:

  • 定义类组件时编写的是React.component(首字母小写),但React v17版本暴露的类组件基类是首字母必须大写的React.Component
  • JS语法对大小写完全敏感,小写的component不属于React对外暴露的有效属性,代码执行到类定义步骤就会抛出TypeError: React.component is not a constructor错误,后续的ReactDOM渲染逻辑不会执行,页面自然没有输出内容。

修正后的完整可运行代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script
      crossorigin
      src="https://unpkg.com/react@17/umd/react.development.js"
    ></script>
    <script
      crossorigin
      src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
    ></script>
    <title>Learning React</title>
  </head>
  <body>
    <div id="root"></div>
    <script>
      // 修正点:将小写的component改为大写C开头的Component
      class App extends React.Component {
        render() {
          return React.createElement("div", null, "Hello World");
        }
      }
      ReactDOM.render(
        React.createElement(App, null),
        document.getElementById("root")
      );
    </script>
  </body>
</html>

排障提示

以后遇到前端页面无预期输出的情况,优先按F12打开浏览器开发者工具的「控制台」面板,所有JS运行报错都会直接显示在面板中,可快速定位问题点。


内容的提问来源于stack exchange,提问作者Usama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.12 19:36:10