0

const root = ReactDOM.createRoot(document.getElementById('root'));
const element = <h1>Hello, 1234world</h1>;
root.render(element);
const root2 = ReactDOM.createRoot(document.getElementById('tworoot'));
const element2 = <h1>Hello, 5678world</h1>;
root2.render(element2);

function MyButton() {
  return (
    <button>
      I'm a button
    </button>
  );
}

export default function MyApp() {
  return (
    <div>
      <h1>Welcome to my app</h1>
      <MyButton />
    </div>
  );
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Add React in One Minute</title>
  </head>
  <body>

    <h2>Add React in One Minute</h2>
    <p>This page demonstrates using React with no build tooling.</p>
    <p>React is loaded as a script tag.</p>

    <!-- We will put our React component inside this div. -->
    <div id="root"></div>
    <div id="tworoot"></div>

    <!-- Load React. -->
    <!-- Note: when deploying, replace "development.js" with "production.min.js". -->
    <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>

    <!-- Load your React component. -->
    <script src="lik.js"></script>

  </body>
</html>

Upon rendering my html page,I am getting console log error as Uncaught SyntaxError: Unexpected token 'export' and my Button is not being displayed.button.please advise me as I think I am nt getting the concepts right

9
  • 2
    You need to add type="module" but it still won't work, because you need babel to transform jsx into js Commented Jan 31, 2023 at 17:47
  • 2
    I preproccessed JSX by running the command.. npx babel --watch src --out-dir . --presets react-app/prod command Commented Jan 31, 2023 at 17:48
  • 2
    Doesn't <script type="module" src="lik.js"></script> fix the issue? Commented Jan 31, 2023 at 18:07
  • 2
    it fixed the error message but the button is not being displayed Commented Jan 31, 2023 at 18:12
  • 2
    Where are you using MyApp? I don't see it appended anywhere. Commented Jan 31, 2023 at 18:15

1 Answer 1

3

In order to render the Button, you have to render the component referencing it, in this case, MyApp.

const root = ReactDOM.createRoot(document.getElementById('root'));
const element = <h1>Hello, 1234world</h1>;
root.render(element);
const root2 = ReactDOM.createRoot(document.getElementById('tworoot'));
const element2 = <MyApp />;
root2.render(element2);
Sign up to request clarification or add additional context in comments.

1 Comment

As an aside, you can also just render the button, without MyApp, if you wanted to, but I didn't want to change what you had, structurally, in case your exports change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.