Your First React Lab

ReactReactIntermediate
Practice Now

Introduction

Hi there, welcome to LabEx! In this first lab, you'll learn the classic "Hello, World!" program in React.

Click the Continue button below to start the lab.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL react(("React")) -.-> react/FundamentalsGroup(["Fundamentals"]) react/FundamentalsGroup -.-> react/jsx("JSX") react/FundamentalsGroup -.-> react/components_props("Components and Props") react/FundamentalsGroup -.-> react/state_lifecycle("State and Lifecycle") subgraph Lab Skills react/jsx -.-> lab-92968{{"Your First React Lab"}} react/components_props -.-> lab-92968{{"Your First React Lab"}} react/state_lifecycle -.-> lab-92968{{"Your First React Lab"}} end

Hello React

Prerequisites

Before we start, we need to make sure that Node.js and npm are installed on LabEx VM.

Open the terminal or command prompt and run the following commands:

cd ~/project
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get update
sudo apt-get install -y nodejs
Nodejs installation terminal screenshot

React Initialization

Let's try to launch a React "Hello World" demo.

Open your terminal or command prompt and run the following command to create a new React project:

cd ~/project
npx create-react-app hello-world-demo

Please wait for the installation to complete.

Update the React App

Next, let's update the default React app to display a "Hello, World!" message.

Open the file src/App.js in your preferred code editor and replace the contents with the following code:

import React from "react";

function App() {
  return (
    <div>
      <h1>Hello, World!</h1>
    </div>
  );
}

export default App;
React Hello World code

By default, the React development server uses port 3000. To change it to port 8080, open the file package.json in your code editor.

Locate the scripts section and update the start script to include the PORT environment variable:

"scripts": {
  "start": "BROWSER=none PORT=8080 react-scripts start",
  ...
}
React package json update

Finally, run the following command to start the React development server:

cd hello-world-demo
npm start
React server start command

That's it! You have successfully launched a React "Hello World" demo on your local server using port 8080.

Now, you can switch to the Web 8080 Tab, and click the refresh button to see the web page.

React Hello World webpage

Summary

Congratulations! You have completed your first LabEx Lab.

If you want to learn more about LabEx and how to use it, you can visit our Support Center . Or you can watch the video to learn more about LabEx.

Programming is a long journey, but Next Lab is just one click away. Let's do it!