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

  1. 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.

  1. Open your terminal or command prompt.

  2. Run the following command to create a new React application:

    npx create-react-app hello-world
    
  3. Navigate to the project directory:

    cd hello-world
    
  4. Start the development server:

    npm start
    

    This will open a new browser window with the default React application running at http://localhost:3000.

  1. 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.

  1. Modifying the App Component

Let's modify the App.js file to display "Hello World".

  1. Open src/App.js in your code editor.

  2. 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;
    
  3. Save the file. The browser should automatically refresh, and you should see "Hello World" displayed on the screen.

  1. 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 named App.
    • return ( ... ): Returns the JSX to be rendered. In this case, it returns a div containing an h1 element with the text "Hello World".
  • Export Statement:

    export default App;
    
    • export default App;: Exports the App 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

  1. Change the message from "Hello World" to "Welcome to React".
  2. Save the file and observe the changes in the browser.

Solution

  1. Open src/App.js.

  2. Modify the h1 element:

    function App() {
      return (
        <div className="App">
          <h1>Welcome to React</h1>
        </div>
      );
    }
    
  3. 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

Module 2: React Components

Module 3: Working with Events

Module 4: Advanced Component Concepts

Module 5: React Hooks

Module 6: Routing in React

Module 7: State Management

Module 8: Performance Optimization

Module 9: Testing in React

Module 10: Advanced Topics

Module 11: Project: Building a Complete Application

© Copyright 2024. All rights reserved