How to Set up React in a HTML File
Most React projects rely on complex build tools. But did you know you can set up simple React components directly within an HTML file? This approach is ideal for rapid prototyping or adding interactive elements to existing pages.
Step 1: Include the Libraries
Add the following CDN scripts inside the <head>
of your HTML file:
HTML
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
- Quick Explanation: (Provide a sentence or two about what each library does – React, ReactDOM, Babel.)
Step 2: Prepare Your HTML
Create a div
to hold your React app:
HTML
<body>
<div id="root"></div>
</body>
Step 3: Write React Code (with Babel)
Embed this inside <script type="text/babel">
tags:
JavaScript
class App extends React.Component {
render() {
return (
<div>
<h1>React Setup</h1>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
Step 4: Production Mode
- Important: Replace the development CDN links with production-ready versions.
Recent Posts
- Heroku vs Azure
- Your Guide to Netlify CMS
- How to use React Icons in Your Projects
- What Is PHP Hosting
- How To Super Charge Learning CSS
- This One Hosting Trick Netlify Doesn’t Want You To Know…
- How To Use SCSS In HTML
- Building the Ultimate React To-Do List: A Step-by-Step Guide
- Different ways to use CSS
- How to Set up a React Webpack Application