React脚本未加载

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

react script not being loaded

问题

我有以下脚本:

app.js

import React from 'react'
import ReactDOM from 'react-dom'
import {Stage, Sprite} from '@inlet/react-pixi';

const App = () => (
    <Stage>
        <Sprite image="assets/baseBike.png" x={100} y={100}/>
    </Stage>
);

ReactDOM.render(<App/>, document.body);

以及以下内容:

index.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Hello World</title>
</head>
<body>
<div id="stageContainer">

</div>
<script src="https://unpkg.com/babel-standalone@6.26.0/babel.min.js"></script>
<script src="node_modules/pixi.js/dist/pixi.min.js"></script>
<script src="app.js" type="text/babel"></script>
</body>
</html>

现在,当我运行时,控制台中没有错误,但没有任何内容被渲染。我也无法找到我的脚本是否真的被加载。

如果我删除 type="text/babel",然后会抛出错误:

Cannot use import statement outside a module

有人能告诉我可能遗漏了什么吗?

更新 Babel

所以我注意到我缺少了 Babel,但在添加它后,我得到以下错误:

require is not defined
英文:

I have the following script:

app.js

import React from &#39;react&#39;
import ReactDOM from &#39;react-dom&#39;
import {Stage, Sprite} from &#39;@inlet/react-pixi&#39;;

const App = () =&gt; (
    &lt;Stage&gt;
        &lt;Sprite image=&quot;assets/baseBike.png&quot; x={100} y={100}/&gt;
    &lt;/Stage&gt;
);

ReactDOM.render(&lt;App/&gt;, document.body);

And the following

index.html

    &lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;title&gt;Hello World&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;stageContainer&quot;&gt;

&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/babel-standalone@6.26.0/babel.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;node_modules/pixi.js/dist/pixi.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;app.js&quot; type=&quot;text/babel&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Now when I run this there is no error in the console but nothing is being rendered. I am also unable to find that my script is actually loaded.

if i remove the type=&quot;text/babel&quot; then it throws me an error:

Cannot use import statement outside a module

Can anyone tell me what I might be missing?

Update babel

So I noticed that I was missing babel however after adding it I get the following error:

 require is not defined

答案1

得分: 3

因为你在 ReactDOM.render 中使用了 document.body,所以你意外删除了 app.js 脚本。

尝试使用一个 div 作为你的挂载点,看看是否解决了你的问题。

例如:

ReactDOM.render(<App />, document.getElementById('stageContainer'));
英文:

I think because you are using document.body on your ReactDOM.render, you accidentally delete the app.js script.

Try to use a div as your mount point to see if that resolves your issue.

e.g.

ReactDOM.render(&lt;App /&gt;, document.getElementById(&#39;stageContainer&#39;));

答案2

得分: 1

由于您正在使用独立的 Babel 与 React,您需要配置您要用来编译脚本的 Babel 预设。

您可能需要类似这样的配置:

<script>
    Babel.registerPreset('myPreset', {
        presets: [[Babel.availablePresets['es2015-loose'], { modules: false }], [Babel.availablePresets['react']]],
        plugins: [[Babel.availablePlugins['transform-modules-umd']]],
    });
</script>

<script type="text/babel" src="app.js" data-presets="myPreset"></script>

请注意,脚本标签上的 data-presets 属性告诉 Babel 使用您定义的预设。

英文:

As you are using babel standalone with React, you need to configure the babel preset that you want to compile your scripts.

You probably need something like this:

	&lt;script&gt;
		Babel.registerPreset(&#39;myPreset&#39;, {
			presets: [[Babel.availablePresets[&#39;es2015-loose&#39;], { modules: false }], [Babel.availablePresets[&#39;react&#39;]]],
			plugins: [[Babel.availablePlugins[&#39;transform-modules-umd&#39;]]],
		});
	&lt;/script&gt;

	&lt;script type=&quot;text/babel&quot; src=&quot;app.js&quot; data-presets=&quot;myPreset&quot;&gt;&lt;/script&gt;

Note that the data-presets property on the script tag tells babel to use your defined preset.

答案3

得分: 1

使用ReactJS与浏览器一起使用Babel时,您不需要使用import语句。以下是我创建的在浏览器中运行的简单应用程序示例(请注意,我没有使用create-react-app):

// app.js
const App = () => {
  return <h1>Test App</h1>;
};
ReactDOM.render(<App />, document.getElementById('root'));

您可以在index.html中使用此组件:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Hello World</title>
  </head>
  <body>
    <!-- 一些HTML内容 -->
    <div id="root"></div>
    <!-- 一些其他HTML内容 -->

    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <!-- 需要babel来解析JSX -->

    <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <!-- 导入React.js -->

    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <!-- 导入react-dom.js -->

    <script type="text/babel" src="/app.js"></script>
    <!-- 导入您的JS文件并添加type="text/babel",否则Babel无法解析它 -->
  </body>
</html>

我已经测试过了,对我有效,希望对您有帮助。

英文:

when using reactjs with browser using babel you dont need to use import ,here is how i have created simple app that run with browser (note that i am not using create-react-app ):
app.js

const App = () =&gt;{
return &lt;h1&gt;Test App&lt;/h1&gt;
 };
ReactDOM.render(&lt;App/&gt;, document.getElementById(&#39;root&#39;));

you can use this component in index.html as

&lt;!doctype html&gt;
   &lt;html&gt;
     &lt;head&gt;
        &lt;meta charset=&quot;utf-8&quot;&gt;
          &lt;title&gt;Hello World&lt;/title&gt;
      &lt;/head&gt;

   &lt;body&gt;
&lt;!-- some HTML --&gt;
&lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
&lt;!-- some other HTML --&gt;

&lt;script src=&quot;https://unpkg.com/babel-standalone@6/babel.min.js&quot;&gt;&lt;/script&gt;
&lt;!-- babel is required in order to parse JSX --&gt;

&lt;script src=&quot;https://unpkg.com/react@16/umd/react.development.js&quot;&gt;&lt;/script&gt;
&lt;!-- import react.js --&gt;

&lt;script src=&quot;https://unpkg.com/react-dom@16/umd/react-dom.development.js&quot;&gt; &lt;/script&gt;
&lt;!-- import react-dom.js --&gt;

&lt;script type=&quot;text/babel&quot; src=&quot;/app.js&quot;&gt;&lt;/script&gt;
&lt;!-- import your JS and add - type=&quot;text/babel&quot; - otherwise babel wont parse it --&gt;
  &lt;/body&gt;
&lt;/html&gt;

i have tested it and its worked for me ,hope it will help you

huangapple
  • 本文由 发表于 2020年1月6日 17:59:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610017.html
匿名

发表评论

匿名网友

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

确定