1

How to import a component in a script in HTML file?
What are the modifications to be done to make the following work?
And should the Babel libraries be added to the HTML file? (Webpack and React are already installed)

In car.js

class Car extends React.Component {
   render() { return (<p>nice car</p>) }}

In page.html

...
<script type="text/babel">
   import Car from "static/car.js"      //<---------????????

   ReactDOM.render(<Car />, document.querySelector("#root"));
</script>

1 Answer 1

2

do not need to import Car from "static/car.js" it will works in JS file.

for HTML need refarance like <script type="text/babel" src="static/car.js"></script>

<script src="static/car.js" type="text/babel"></script>
<script type="text/babel">
  ReactDOM.render(<Car />, document.querySelector("#root"));
</script>

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

2 Comments

It doesn't work. it gives errors: react is not defined, Unexpected token '<' , Car is not defined.
@Silver please add ` type="text/babel"` in script tag

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.