-1

I have added a React functional component with existing HTML page, getting a strange error : Uncaught SyntaxError: Unexpected token '<'

HTML


<html lang="en">
<body>
    <div id="like_button_container"></div>

    <script src="https://unpkg.com/react@16.8/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16.8/umd/react-dom.development.js" crossorigin></script>

    <!-- Load our React component. -->
    <script src="like_button.js"></script>

</body>
</html>

connected JS

"use strict";

function Like_button() {
  const [like, setLike] = React.useState(false);
  return (
    <div>
      {like ? <p>you liked t</p> : null}
      <button onClick={() => setLike(true)}>click</button>
    </div>
  );
}


const domContainer = document.querySelector("#like_button_container");
ReactDOM.render(React.createElement(Like_button), domContainer);

enter image description here

Anoop K George
  • 1,605
  • 12
  • 40

2 Answers2

1

trying replacing this in the html:

from:

<script src="like_button.js"></script>

to:

<script type="text/jsx" src="./like_button.js"></script>

you need to tell it to use jsx

Red Baron
  • 7,181
  • 10
  • 39
  • 86
1

first, you should add babel-core CDN.

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script> then add text/babel to your script like this:

<div id="like_button_container"></div>
<script type="text/babel" src="./like_button.js"></script>