0

I am a beginner in react. When I render elements with createElement, it works fine. But rendering like below with JSX, it throws and error saying Uncaught SyntaxError: Unexpected token <

const { createElement } = React;
const { render } = ReactDOM; 

const style = {
    fontFamily: 'Arial',
    color: 'White',
    backgroundColor: 'Red',
    padding: '10px',
    border: '10px solid black'
}

render(
    <h1 id="emp-title" className="header" style={style}>Full time Employee</h1>,
    document.getElementById('react-container-jsx')
);
<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
        <title>React 101</title>
    </head>
    <body>
        <div id="react-container-jsx"></div>
        <script src="index.js"></script>
    </body>
</html>

Attempts: tried but did not work

<script src="index.js" type = "text/babel"></script>

Also

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

Didn't work. Any help is much appreciated.

1 Answer 1

2

You will need to link babel-standalone as well

And add type="text/babel" to the script tag.

Working snippet:

const { createElement } = React;
const { render } = ReactDOM; 

const style = {
    fontFamily: 'Arial',
    color: 'White',
    backgroundColor: 'Red',
    padding: '10px',
    border: '10px solid black'
}

render(
    <h1 id="emp-title" className="header" style={style}>Full time Employee</h1>,
    document.getElementById('react-container-jsx')
);
<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
        <script src="https://unpkg.com/[email protected]/babel.min.js"></script>
        <title>React 101</title>
    </head>
    <body>
        <div id="react-container-jsx"></div>
        <script src="index.js" type="text/babel"></script>
    </body>
</html>

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

4 Comments

Thanks for the answer. Tried Just now. But still does not work. Can you share a working snippet.
Thanks for the link. Along with the babel js, I also had to add the type="text/babel" in order to make it work.
Yeah type="text/babel" is needed, I thought you already know that as you mentioned it in your question.

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.