0

I've been studying React and now I want to try implementing it on some pages only, as a proof-of-concept to my supervisor at work. How do I do that using old-school script tags, instead of imports?

I've been able to use the in-browser babel transpiler

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
  </head>
  <body>
    <div id="output"></div>
    <script type="text/babel">
      ReactDOM.render(
        <div>Hello world!!</div>,
        document.getElementById("output")
      );
    </script>
  </body>
</html>

This works, but how do I create react components here? Creating a class that extends Component will throw an undefined error. How do I do import React, {Component} from 'react'; using this method? And how do I use components in external jsx files?

1 Answer 1

2

Why not?

<script type="text/babel">
  // Define children components here

  class App extends React.Component {
    render() {
      return (<div>Hello world</div>)
    }
  }

  ReactDOM.render(
    <App />,
    document.getElementById("output")
  );
</script>

UPD

For a few components, just write down children components before parent one (look at comment in my code). If you have a lot of components, start to use builders like webpack

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

1 Comment

Oh okay it works, I just needed to use React.Component. How about including external component jsx files?

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.