简体   繁体   中英

Is there a way to import modules in React without create-react-app?

I've just started using react and was using the create-react-app from npm until today. I've gotten the react and reactdom code from

https://unpkg.com/react@15/dist/react.js
https://unpkg.com/react-dom@15/dist/react-dom.js

I've saved the files as:

/Test/react/react.js
/Test/react/react-dom.js

This is my index.js:

<!--/Test/index.js-->

<html>

<head>
    <title>React Hello World</title>
</head>

<body>
    <div id="root"></div>

    <script src="react/react.js"></script>
    <script src="react/react-dom.js"></script>
    <script src="App.js"></script>
</body>

</html>

I have created a module called Greetings, given below:

//Test/Components/Greetings.js

class Greetings extends React.Component {
  render() {
    return React.createElement(
      "h1",
      null,
      "Greetings, " + this.props.name + "!"
    );
  }
}

export default Greetings;

And I've created an App.js, given here:

//Test/App.js

import Greetings from "./Components/Greetings";

ReactDOM.render(
  React.createElement(Greetings, { name: "Me" }),
  document.getElementById("root")
);

I'm getting this error:

Uncaught SyntaxError: Unexpected identifier

Is there an easy way to import a react module?

import and export are Node.js keywords. On the client-side you don't need them. Simply include your javascript files in the html:

<script src="App.js"></script>
<script src="Greetings.js"></script>

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