Website
Last updated
Was this helpful?
Last updated
Was this helpful?
React has been designed from the start for gradual adoption, and you can use as little or as much React as you need. Whether you’re working with micro-frontends, an existing system, or just giving React a try, you can start adding interactive React components to an HTML page with just a few lines of code—and no build tooling!
You can add a React component to an existing HTML page in under a minute. Try this out with your own website or —all you need is an internet connection and a text editor like Notepad (or VSCode—check out our guide on )!
In the HTML page you want to edit, add an HTML element like an empty <div>
tag with a unique id
to mark the spot where you want to display something with React.
You can place a “container” element like this <div>
anywhere inside the <body>
tag. React will replace any existing content inside HTML elements, so they are usually empty. You can have as many of these HTML elements on one page as you need.
In the HTML page, right before the closing </body>
tag, add three <script>
tags for the following files:
Create a file called like_button.js next to your HTML page, add this code snippet, and save the file. This code defines a React component called LikeButton
.
Lastly, add two lines to the bottom of like_button.js. These two lines of code find the <div>
you added to your HTML in the first step and then display the “Like” button React component inside of it.
Congratulations! You have just rendered your first React component to your website!
You might want to display a React component in multiple places on the same HTML page. This is most useful while React-powered parts of the page are isolated from each other. You can do this by calling ReactDOM.render()
multiple times with multiple container elements.
In index.html, add an additional container element <div id="component-goes-here-too"></div>
.
In like_button.js, add an additional ReactDOM.render()
for the new container element:
Unminified JavaScript can significantly slow down page load times for your users. Before deploying your website to production, it’s a good idea to minify its scripts.
If you already minify your application scripts, your site will be production-ready if you ensure that the deployed HTML loads the versions of React ending in production.min.js
like so:
The examples above rely on features that are natively supported by browsers. This is why like_button.js uses a JavaScript function call to tell React what to display:
These two code snippets are equivalent. JSX is popular syntax for describing markup in JavaScript. Many people find it familiar and helpful for writing UI code—both with React and with other libraries. You might see “markup sprinkled throughout your JavaScript” in other projects!
The quickest way to try JSX in your project is to add the Babel compiler to your page’s <head>
along with React and ReactDOM like so:
Now you can use JSX in any <script>
tag by adding type="text/babel"
attribute to it. For instance:
To convert like_button.js to use JSX:
In like_button.js, replace
with:
In index.html, add type="text/babel"
to the like button’s script tag:
This approach is fine for learning and creating simple demos. However, it makes your website slow and isn’t suitable for production. When you’re ready to move forward, remove this new <script>
tag and the type="text/babel"
attributes you’ve added. Instead, in the next section you will set up a JSX preprocessor to convert all your <script>
tags automatically.
npm install babel-cli@6 babel-preset-react-app@3
You only need npm to install the JSX preprocessor. You won’t need it for anything else. Both React and the application code can stay as <script>
tags with no changes.
Congratulations! You just added a production-ready JSX setup to your project.
You can preprocess JSX so that every time you save a file with JSX in it, the transform will be re-run, converting the JSX file into a new, plain JavaScript file.
Create a folder called src
In your terminal, run this command: npx babel --watch src --out-dir . --presets react-app/prod
(Don’t wait for it to finish! This command starts an automated watcher for JSX.)
The watcher will create a preprocessed like_button.js with the plain JavaScript code suitable for the browser.
Check out !
If you don’t have a minification step for your scripts, .
However, React also offers an option to use , an HTML-like JavaScript syntax, instead:
Here is that you can download and play with.
Adding JSX to a project doesn’t require complicated tools like a or a development server. Adding a JSX preprocessor is a lot like adding a CSS preprocessor.
Go to your project folder in the terminal, and paste these two commands (Be sure you have installed!):
npm init -y
(if it fails, )
Move your JSX-ified like_button.js to the new src folder (or create a like_button.js containing this )
As a bonus, this also lets you use modern JavaScript syntax features like classes without worrying about breaking older browsers. The tool we just used is called Babel, and you can learn more about it from .
If you’re getting comfortable with build tools and want them to do more for you, .