It works perfectly because I haven't included JSX, but when I replace the script type with text/babel
, it doesn't work because the module fails to load. browser.js
the Babel compiler.
Here... JSX works only when i replace script type with text/babel
but the problem is module fails to load since the script is not module. Any idea how make it work with JSX?
<div id="root">
</div>
<script type="module">
import './react.min.js';
import './react-dom.min.js';
import './browser.js';
class Hello extends React.Component {
render() {
return React.createElement('div', null, `Hello ${this.props.toWhat}`);
}
}
ReactDOM.render(
React.createElement(Hello, {toWhat: 'World'}, null),
document.getElementById('root')
);
</script>
Answers
To make JSX work in your HTML file without using text/babel
, you'll need to precompile your JSX code into regular JavaScript using Babel before including it in your script tag. Here's how you can achieve that:
Step-by-Step Approach:
-
Install Babel CLI and Presets: First, make sure you have Babel CLI installed globally or locally in your project along with the necessary presets for compiling JSX.
npm install --save-dev @babel/core @babel/cli @babel/preset-react
-
Create a
.babelrc
Configuration File: Create a.babelrc
file in the root of your project (or configure inpackage.json
) with the presets needed for compiling JSX.{ "presets": ["@babel/preset-react"] }
-
Compile JSX to JavaScript: Use Babel CLI to compile your JSX file into a JavaScript file that can be directly used in your HTML.
npx babel your-script.jsx --out-file your-script.js
This command compiles
your-script.jsx
(containing your JSX code) intoyour-script.js
. -
Modify Your HTML: Update your HTML file to include the compiled JavaScript file instead of the JSX directly.
<div id="root"></div> <script src="path/to/your-script.js"></script>
Example JSX to JavaScript Compilation
Assuming your JSX code is in your-script.jsx
, here’s how you compile it using Babel:
-
Create
your-script.jsx
:import React from 'react'; import ReactDOM from 'react-dom'; class Hello extends React.Component { render() { return React.createElement('div', null, `Hello ${this.props.toWhat}`); } } ReactDOM.render( React.createElement(Hello, { toWhat: 'World' }, null), document.getElementById('root') );
-
Compile
your-script.jsx
toyour-script.js
:npx babel your-script.jsx --out-file your-script.js
-
Update your HTML:
<div id="root"></div> <script src="your-script.js"></script>
Explanation
- Babel: Babel is a JavaScript compiler that transforms JSX syntax into regular JavaScript that browsers can understand.
- @babel/preset-react: Preset for Babel that includes plugins necessary for handling React's JSX syntax.
- Compilation: By compiling JSX to JavaScript beforehand, you avoid the need for
text/babel
in the script type, allowing your HTML to work with standard script loading (type="module"
ortype="text/javascript"
).
This approach ensures compatibility with different browsers and avoids relying on text/babel
, which is primarily intended for development environments or online compilers rather than production use due to its runtime compilation overhead.