Introduction
In this lesson, we will create our first React application and display a simple "Hello World" message. This will help you understand the basic structure of a React application and how to render elements to the DOM.
Prerequisites
Before starting, ensure you have:
- Node.js and npm installed on your machine.
- A code editor (e.g., Visual Studio Code).
Steps to Create a "Hello World" Application
- Setting Up the Project
First, we need to set up a new React project using Create React App, a tool that sets up a new React project with a sensible default configuration.
-
Open your terminal or command prompt.
-
Run the following command to create a new React application:
npx create-react-app hello-world
-
Navigate to the project directory:
cd hello-world
-
Start the development server:
npm start
This will open a new browser window with the default React application running at
http://localhost:3000
.
- Understanding the Project Structure
The Create React App tool generates a project with the following structure:
hello-world/ ├── node_modules/ ├── public/ │ ├── index.html │ └── ... ├── src/ │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── index.css │ ├── index.js │ └── ... ├── .gitignore ├── package.json ├── README.md └── ...
Key files to note:
public/index.html
: The HTML file that serves as the entry point for the React application.src/index.js
: The JavaScript file that renders the React application to the DOM.src/App.js
: The main component of the React application.
- Modifying the App Component
Let's modify the App.js
file to display "Hello World".
-
Open
src/App.js
in your code editor. -
Replace the existing code with the following:
import React from 'react'; import './App.css'; function App() { return ( <div className="App"> <h1>Hello World</h1> </div> ); } export default App;
-
Save the file. The browser should automatically refresh, and you should see "Hello World" displayed on the screen.
- Understanding the Code
Let's break down the code in App.js
:
-
Import Statements:
import React from 'react'; import './App.css';
import React from 'react';
: Imports the React library, which is necessary to use JSX.import './App.css';
: Imports the CSS file for styling the component.
-
App Component:
function App() { return ( <div className="App"> <h1>Hello World</h1> </div> ); }
function App() { ... }
: Defines a functional component namedApp
.return ( ... )
: Returns the JSX to be rendered. In this case, it returns adiv
containing anh1
element with the text "Hello World".
-
Export Statement:
export default App;
export default App;
: Exports theApp
component as the default export of the module, allowing it to be imported and used in other files.
Practical Exercise
Exercise 1: Modify the Message
- Change the message from "Hello World" to "Welcome to React".
- Save the file and observe the changes in the browser.
Solution
-
Open
src/App.js
. -
Modify the
h1
element:function App() { return ( <div className="App"> <h1>Welcome to React</h1> </div> ); }
-
Save the file. The browser should now display "Welcome to React".
Conclusion
In this lesson, you learned how to set up a new React project and create a simple "Hello World" application. You also gained an understanding of the basic structure of a React application and how to render elements to the DOM. In the next lesson, we will dive deeper into JSX, the syntax extension used in React to describe UI elements.
React Course
Module 1: Introduction to React
- What is React?
- Setting Up the Development Environment
- Hello World in React
- JSX: JavaScript Syntax Extension
Module 2: React Components
- Understanding Components
- Functional vs Class Components
- Props: Passing Data to Components
- State: Managing Component State
Module 3: Working with Events
Module 4: Advanced Component Concepts
- Lifting State Up
- Composition vs Inheritance
- React Lifecycle Methods
- Hooks: Introduction and Basic Usage
Module 5: React Hooks
Module 6: Routing in React
Module 7: State Management
- Introduction to State Management
- Context API
- Redux: Introduction and Setup
- Redux: Actions and Reducers
- Redux: Connecting to React
Module 8: Performance Optimization
- React Performance Optimization Techniques
- Memoization with React.memo
- useMemo and useCallback Hooks
- Code Splitting and Lazy Loading
Module 9: Testing in React
- Introduction to Testing
- Unit Testing with Jest
- Testing Components with React Testing Library
- End-to-End Testing with Cypress
Module 10: Advanced Topics
- Server-Side Rendering (SSR) with Next.js
- Static Site Generation (SSG) with Next.js
- TypeScript with React
- React Native: Building Mobile Apps