简体   繁体   中英

Can't Import React

Can't Import React

When I try to import react it throws me an error saying that react is invaldid an "unexpected identifier"

#############################################################################
//index.html

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="root"></div>
        <script src="index.js"></script>
    </body>
</html>

#############################################################################
//index.js

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

    ReactDOM.render(<h1>Hello World</h1>, document.getElementById("root"));

#############################################################################


ERROR: Uncaught SyntaxError: Unexpected identifier

Browser won't understand import statements, you have to transpile code using babel and bundle using webpack . Also please try to use react starter kits eg create-react-app which are configured using babel and webpack

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