This tutorial will show you how to use React and build your first web application using it.
Before we begin, you must know what JSX is, as React uses it to function properly.
var element = <h1>The quick brown fox jumps over the lazy dog</h1>;
Because this tutorial is for beginners, we are going to start simple. We are only going to use two files:
index.js(or whatever you want it to be named)
First, we are going to set up the HTML file. All we need to do is create a basic webpage, create a
<div> element inside of the
<body> element, and load the React library. We are going to use a CDN, so we don't have to download the full library.
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
The versions above are meant for development, and not for production. Minified and optimized production versions of React are also available:
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
For this tutorial, we are going to use the development version.
The HTML file should look like this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>My First React App</title> </head> <body> <div id="root"></div> <!-- Reference the React library here --> <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <!-- Reference JS file here --> <script src="index.js"></script> </body> </html>
ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('root') );
Here is the same example, but without JSX:
ReactDOM.render( React.createElement('h1', null, 'Hello, world!'), document.getElementById('root') );
Your website should look like this:
Congratulations! You have just learned to create your very first web app using React! I hope you enjoyed this tutorial!