0

I'm trying to render react components from an html page. I referenced the question React - component in seperate script does not work and loaded the html file from a URL like http://localhost/index.html. My index.html:

<!DOCTYPE html>
   <html>
       <head>
           <script src="https://...fb.me/react-0.13.3.js"></script>
           <script src="https://...fb.me/JSXTransformer-0.13.3.js"></script>
       </head>
       <body>
           <div id="example"></div>
           <script type="text/jsx" src="index.js"></script>
       </body>
   </html>

My index.js:

 ReactDOM.render(<MainPage />, document.getElementById('example'))

The error is that MainPage is not defined. But when I write

include MainPage

There is an error on the include statement. Also, how can i include other React libraries like 'Tabs' from react-tabs?

2
  • 4
    0.13.3 yikes! Commented Aug 28, 2019 at 15:49
  • Hope you're not intending to use this in production Commented Aug 28, 2019 at 16:10

2 Answers 2

1

If you index.js which is loaded inside your html contains <MainPage />, that means you did not compile your code. Browsers do not support text/jsx natively.

In order to use react with JSX you will need to use a compiler / bundler like webpack.

Sign up to request clarification or add additional context in comments.

Comments

0

As you are just learning React.Js for the first time, please consider @Kapil's answer:

<!DOCTYPE html>
<html>
  <head>
   <script crossorigin src="https://unpkg.com/react@15/dist/react.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
<script crossorigin src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
    ReactDOM.render(
      <h1>Hello, world!</h1>,
      document.getElementById('example')
    );
</script>
  </head>
  <body>
    <div id="example"></div>
  </body>
</html>

Comments

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.