简体   繁体   English

不知道为什么我的 React 组件没有渲染?

[英]Can't figure out why my React Component isn't rendering?

Is there an issue with how I imported react or am I just missing something small?我如何导入 react 是否存在问题,或者我只是缺少一些小东西? Any feedback would be greatly appreciated.任何反馈将不胜感激。 I'm pretty new to programming so there's probably a really simple solution here that I'm just not informed enough to see.我对编程很陌生,所以这里可能有一个非常简单的解决方案,我只是没有足够的了解来查看。 Thanks.谢谢。

Edit: After taking out the commented out portion of the body, the component still doesn't render.编辑:取出主体的注释掉部分后,组件仍然不呈现。 I get this error我收到这个错误

Uncaught SyntaxError: Unexpected token '<' (at index.html:22:13)未捕获的 SyntaxError:意外的令牌“<”(在 index.html:22:13)

Can the file not understand that I'm trying to use a react component?文件不能理解我正在尝试使用反应组件吗? Thanks.谢谢。

2nd edit: Solved it, I was missing type="text/babel" in the script element.第二次编辑:解决了,我在脚本元素中缺少 type="text/babel" 。

    <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- CSS only -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">


    <!-- React Import -->
    <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

    <title>Markdown Previewer</title>
  </head>

    <script>
      class App extends React.Component {
        render() {
          return(
            <div class="">
            Hi
            </div>
          );
        }
      }

      ReactDOM.render(<App/>,
      document.getElementById('app'));
    </script>

  <body>
    <!--<wrapper>
      <div id="app"></div>
      <textarea name="name" rows="8" cols="80" id="editor"></textarea>
      <div id="preview"></div>
    </wrapper>-->
    <!-- JavaScript Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>
    <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>



  </body>
</html>

remove comment braces: <!-- -->删除注释大括号: <!-- -->

and write just:只写:

<wrapper>
  <div id="app"></div>
  <textarea name="name" rows="8" cols="80" id="editor"></textarea>
  <div id="preview"></div>
</wrapper>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM