2

I'm working on a project, which has already a webserver. The webserver runs PHP and it is limited. Many software cannot be installed.

I understand that React does not require any installation but adding JSX to a project needs to run these commands:

Step 1: Run npm init -y

Step 2: Run npm install babel-cli@6 babel-preset-react-app@3

Unfortunately, I cannot add NodeJS to the webserver.

JSX coding syntax would be very useful for me using the JavaScript files.

Option for adding it to HTML

I understand that I could add JSX to an HTML file:

<script type="text/babel">
  ReactDOM.render(
    <h1>Hello, world!</h1>,
    document.getElementById('root')
  );
</script>

But I would like to use in .js files.

Examples

I have 2 examples:

  1. Example is working but it has no JSX.
  2. Example has JSX but it is not working.

The HTML file is the same.

1. Example project: Unexpected token error with JSX

const myelement1 = <h1>JSX should work</h1>;

ReactDOM.render(
  myelement1,
  document.getElementById('reactRoot')
);
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>

  </head>
  <body>
    <h1>My React example page</h1>
    <div id="reactRoot"></div>

    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

  </body>
</html>

2. Example project: React is working without JSX

const myelement2=React.createElement('h1', {}, 'No JSX!');

ReactDOM.render( myelement2, document.getElementById('reactRoot'));
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>

  </head>
  <body>
    <h1>My React example page</h1>
    <div id="reactRoot"></div>

    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

  </body>
</html>

My Question

How could I fix 1. Example project, if no program installation is possible?

1 Answer 1

1

you have to set type attribute as text/babel, this way the problems will be resolved, e.g:

<html>

<body>
    <div id="reactRoot"></div>

    <script type="text/babel">
        const myelement1 = <h1>JSX should work</h1>;

        ReactDOM.render(
            myelement1,
            document.getElementById('reactRoot')
        );
    </script>

    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

</body>

</html>

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

6 Comments

I need to store the JSX in .js files. This is 1 HTML file that included the JSX. Do you have any idea for a separately stored .js file?
@questionMark that does not matter, just add type="text/babel" to your script tag, like this: <script type="text/babel" src="./scripts/jsx.js"></script>
I tried your recommended script tag locally, added to the .html. It was not working. Is this working for you?
What @PS-PARSA is suggesting should work I just tested it.
@questionMark this error is related to CORS policy, not jsx and Babbel...
|

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.