Unable to import JS files with a project using CDN links (react, reactDOM)
Asked Answered
P

1

0

I want to import subfiles (Header.js; Footer.js, MainContent.js) into index.js instead of coding similar subfunctions in index.js file. I put the functions in index.js and it worked but splitting the file and importing it didn't. When I write the import, it doesn't work and doesn't show the elements. There ara list of my folders and code.

(index.js)

  import Header from "./Header";
    import Footer from "./Footer";
    import MainContent from "./MainContent";
    
    function App() {
      return (
        <div>
          <Header />
          <MainContent />
          <Footer />
        </div>
      );
    }
    
    ReactDOM.render(<App />, document.getElementById("root"));

(index.html)

  <html>
      <head>
        <link rel="stylesheet" href="style.css" />
        <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>
      </head>
      <body>
        <div id="root"></div>
        <script src="index.js" type="text/babel"></script>
      </body>
    </html>

(Header.js)

 export default function Header() {
      return (
        <header>
          <nav className="nav">
            <img src="./react-logo.png" className="nav-logo" />
            <ul className="nav-items">
              <li>Pricing</li>
              <li>About</li>
              <li>Contact</li>
            </ul>
          </nav>
        </header>
      );
    }

(MainContent.js)

 export default function MainContent() {
      return (
        <div>
          <h1>Reasons I'm excited to learn React</h1>
          <ol>
            <li>
              It's a popular library, so I'll be able to fit in with the cool kids!
            </li>
            <li>I'm more likely to get a job as a developer if I know React</li>
          </ol>
        </div>
      );
    }

(Footer.js)

export default function Footer() {
  return (
    <footer>
      <small>© 2021 Ziroll development. All rights reserved.</small>
    </footer>
  );
}

Please help me with the answer, thanks

Phosphoprotein answered 18/9, 2022 at 4:47 Comment(2)
There is nothing wrong with the way you've split your code up and imported it. The only thing that comes to mind is I believe I once read somewhere that the CDN imports need to specify a specific React version. Try "..../[email protected]/...." and "..../[email protected]/...." in both source urls.Cayuga
I tried but unfortunately still not working. Anyway thank you nahPhosphoprotein
P
0

I found the solution and and it is noted where it is added below:

(index.html)

<html>
<head>
<link rel="stylesheet" href="style.css" />
<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>

<!--This must be added to the file for each imported function-->
<!-- Here for Header.js-->
<script
  data-plugins="transform-es2015-modules-umd"
  type="text/babel"
  src="./Header.js"
></script>
<!-- Here for MainContent.js-->
<script
  data-plugins="transform-es2015-modules-umd"
  type="text/babel"
  src="./MainContent.js"
></script>
<!-- Here for Footer.js-->
<script
  data-plugins="transform-es2015-modules-umd"
  type="text/babel"
  src="./Footer.js"
></script>

<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>

<body>
<div id="root"></div>
<!-- Final, I have to add data-plugins here -->
<script
  src="index.js"
  type="text/babel"
  data-plugins="transform-es2015-modules-umd"
></script>
</body>
</html>

Everything works after adding the above script

Phosphoprotein answered 30/9, 2022 at 0:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.