为什么 import React, ReactDOM 不起作用

huangapple go评论70阅读模式
英文:

Why is import React, ReactDOM not working

问题

In my index.js页面中,我有以下代码:

import React from "react";
import ReactDOM from "react-dom";

const page = (
    <div>
        <h1 className="header">This is JSX</h1>
        <p>This is a paragraph</p>
    </div>
)
console.log(page)
const container = document.getElementById("root");
ReactDOM.render(
    page,
    container
);

在我的index.html中,我有以下内容:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- <script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> -->

    <title>Document</title>
</head>

<body>
    <div id="root"></div>
    <script src="index.js"></script>
</body>

</html>

如果要解决在本地主机上未呈现元素的问题,请取消注释以下代码:

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

然后移除您的导入语句。

英文:

In my index.js page I have

import React from &quot;react&quot;;
import ReactDOM from &quot;react-dom&quot;;

const page = (
    &lt;div&gt;
        &lt;h1 className=&quot;header&quot;&gt;This is JSX&lt;/h1&gt;
        &lt;p&gt;This is a paragraph&lt;/p&gt;
    &lt;/div&gt;
)
console.log(page)
const container = document.getElementById(&quot;root&quot;);
ReactDOM.render(
    page,
    container
);

In my index.html I'm having

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;!-- &lt;script crossorigin src=&quot;https://unpkg.com/react@18/umd/react.development.js&quot;&gt;&lt;/script&gt;
    &lt;script crossorigin src=&quot;https://unpkg.com/react-dom@18/umd/react-dom.development.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://unpkg.com/babel-standalone@6/babel.min.js&quot;&gt;&lt;/script&gt; --&gt;

    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
    &lt;script src=&quot;index.js&quot;&gt;&lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

Im not getting the elements rendered in my localhost.
It works if I uncomment
&lt;!-- &lt;script crossorigin src=&quot;https://unpkg.com/react@18/umd/react.development.js&quot;&gt;&lt;/script&gt;
&lt;script crossorigin src=&quot;https://unpkg.com/react-dom@18/umd/react-dom.development.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/babel-standalone@6/babel.min.js&quot;&gt;&lt;/script&gt; --&gt;

and remove my imports.

Please help check in this issue.

答案1

得分: 2

这是一个关于如何在不使用Webpack、Rollup或Vite等打包工具的情况下,将React作为UMD使用的示例,同时使用了独立的Babel。

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Home</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <script
      crossorigin
      src="https://unpkg.com/react@16.14.0/umd/react.development.js"
    ></script>
    <script
      crossorigin
      src="https://unpkg.com/react-dom@16.14.0/umd/react-dom.development.js"
    ></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel" src="script.js"></script>
  </body>
</html>

script.js:

const Page = () => (
  <div>
    <h1 className="header">This is JSX</h1>
    <p>This is a paragraph</p>
  </div>
);

const container = document.getElementById('root');
ReactDOM.render(<Page />, container);

stackblitz

英文:

It seems you want to use React as a UMD with Babel standalone and without any bundle tools like Webpack, Rollup, and Vite.

A working example using react@16.14.0

index.html:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;title&gt;Home&lt;/title&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width&quot; /&gt;
    &lt;script
      crossorigin
      src=&quot;https://unpkg.com/react@16.14.0/umd/react.development.js&quot;
    &gt;&lt;/script&gt;
    &lt;script
      crossorigin
      src=&quot;https://unpkg.com/react-dom@16.14.0/umd/react-dom.development.js&quot;
    &gt;&lt;/script&gt;
    &lt;script src=&quot;https://unpkg.com/@babel/standalone/babel.min.js&quot;&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
    &lt;script type=&quot;text/babel&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

script.js:

const Page = () =&gt; (
  &lt;div&gt;
    &lt;h1 className=&quot;header&quot;&gt;This is JSX&lt;/h1&gt;
    &lt;p&gt;This is a paragraph&lt;/p&gt;
  &lt;/div&gt;
);

const container = document.getElementById(&#39;root&#39;);
ReactDOM.render(&lt;Page /&gt;, container);

stackblitz

huangapple
  • 本文由 发表于 2023年5月13日 10:40:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定