Ask Learn Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The most common way to start any programming course is to display the text "Hello, world!" Continuing with this tradition, we'll use React to display the famous text.
We'll create two items for the base of our project:
In Visual Studio Code, create a new file in the public folder. Name it index.html.
Add the following HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Recipes</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/dist/index.js"></script>
</body>
</html>
Notice two important lines in our HTML:
<div id="app"></div>
<script type="module" src="/dist/index.js"></script>
Note
The name of the file we're importing is index.js. We don't use index.jsx, because browsers can't render JSX files. We always need a bundler (such as Snowpack) to generate JavaScript. We reference the JavaScript rather than the JSX.
The type="module"
attribute allows us to use import
statements in our JavaScript (or JSX) files. This functionality is relatively new to browsers. It helps us import the necessary packages and components.
We need code to render our React application inside the HTML. Traditionally, the index.jsx file is used to render the application.
Create a new file inside the src folder. Name it index.jsx.
Add the following code:
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('app')
);
Our index.jsx file starts by importing two important libraries. The first library, React
, allows us to use JSX. It will be imported in every component or JSX file we create. The second library, ReactDOM
, renders our application inside the HTML.
render
takes two parameters:
h1
heading.app
. We created this element earlier.The ability to use HTML inside our JavaScript coding is part of the power of JSX.
Now that we've created our code, let's see our site in action!
Open the integrated terminal in Visual Studio Code by selecting View > Terminal or by selecting Ctrl+`. On a Mac, select Cmd+` instead.
Use the following command to start Snowpack's development server:
npm start
Your default browser should automatically open and display your page. If the page doesn't appear automatically, open your browser and go to http://localhost:8080. You should now see your page!
Our JSX code is converted into the appropriate HTML and JavaScript for display in the browser. Open the JavaScript file generated by Snowpack: http://localhost:8080/dist/index.js. You'll see the following code:
import React from "../web_modules/react.js";
import ReactDOM from "../web_modules/react-dom.js";
ReactDOM.render(/* @__PURE__ */ React.createElement("h1", null, "Hello, world!"), document.getElementById("app"));
Focus on the line of code that generates the h1
element and places the text inside it:
React.createElement("h1", null, "Hello, world!")
Using this code is similar to using document.createElement
with vanilla JavaScript. The tooling provided by Snowpack and other bundlers allows us to use JSX to automatically generate the appropriate browser-friendly code.
Please sign in to use this experience.
Sign in