Rapid Experimenting with JSX
The fastest approach to incorporate JSX into your project involves including this <script> tag on your page:
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
With this addision, you can utilize JSX within any <script> element by setting the attribute type="text/babel". Here's an example HTML file demonstrating JSX usage that you can download and experiment with.
Comparison between using jsx and vanilla JavaScript:
<html>
<head>
<meta charset="UTF-8" />
<title>Adding React to Website</title>
<script src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<!-- Avoid in production: Babel required for JSX syntax -->
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class ExampleComponent extends React.Component {
constructor(props) {
super(props);
this.componentState = {
items: [1, 2, 3]
}
}
componentWillMount() {
console.log("componentWillMount: Component about to mount.");
}
componentDidMount() {
console.log("componentDidMount: Component mounted successfully, now visible on page.");
}
componentWillReceiveProps() {
console.log("componentWillReceiveProps: Component about to receive new props.");
}
shouldComponentUpdate() {
console.log("shouldComponentUpdate: Determine if component needs updating. Not yet updated.");
return true;
}
componentWillUpdate() {
console.log("componentWillUpdate: Component about to update.");
}
componentDidUpdate() {
console.log("componentDidUpdate: Page has been re-rendered.");
}
componentWillUnmount() {
console.log("componentWillUnmount: Component about to be unmounted.");
}
render() {
return (
<div id="application">
{
this.componentState.items.map((element, position) => {
return <h1 key={position}>{element}</h1>
})
}
</div>
)
}
};
ReactDOM.render(
<ExampleComponent />,
document.querySelector('#root')
);
</script>
<!--
Note: this page is a great way to try React but it's not suitable for production.
It slowly compiles JSX with Babel in the browser and uses a large development build of React.
Read this section for a production-ready setup with JSX:
https://reactjs.org/docs/add-react-to-a-website.html#add-jsx-to-a-project
In a larger project, you can use an integrated toolchain that includes JSX instead:
https://reactjs.org/docs/create-a-new-react-app.html
You can also use React without JSX, in which case you can remove Babel:
https://reactjs.org/docs/react-without-jsx.html
-->
</body>
</html>