简体   繁体   中英

Calling a .js file (react) from index.html

Im having difficulty to call my js script from my index.html file. I'm guessing it's because I am not calling it like I should but I can't find the solution online.

Basically I have a index.html file

<!DOCTYPE html>
  <html>
    <head>
      <meta charset="utf-8">
    </head>

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

And also a script.js file in the same folder

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

ReactDOM.render(

    <h1>This is my script</h1>

);

My goal here is to run my js file which will then run my entire react app. Thanks a lot for any help you can give me.

(New to React, trying to run my app locally without having to use node.js)

I think you just need to use the Babel transpiler to translate from ES6 to a version of javascript your browser understands. That's the standard way of using ES6 features in the browser.

Many projects use Babel as part of their build pipeline, although you can also do the translation at runtime. The official React documentation uses Babel Standalone in their "Hello world" examples.

Browsers don't support import feature.

I don't think it's posible to develop a modern app without node. If it's posible it won't be easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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