DEV Community

Cover image for Introduction to React: A Beginner's Guide
Samuel C. Okanume
Samuel C. Okanume

Posted on

Introduction to React: A Beginner's Guide

Table of Contents

  1. What is React?

  2. Why use React?

  3. React Basics

  4. Your First React Application

  5. Summary

1. What is React?

React is a popular open-source JavaScript library for building user interfaces, particularly for single-page applications. It's used for handling the view layer of web and mobile apps. React was created by a software engineer at Facebook and it was first deployed on Facebook's news feed in 2011 and on Instagram in 2012.

React allows developers to create large web applications that can change data, without reloading the page. The main purpose of React is to be fast, scalable, and simple. It works only on the user interface in an application, which is also known as the view in the MVC (Model-View-Controller) software design pattern.

React allows developers to create reusable UI components, which is a significant benefit as it enables the development of complex applications to be more straightforward to maintain.

2. Why use React?

Here are some reasons why developers choose to use React:

  • Component-Based: React is all about components - basically, custom HTML elements - with which you can quickly build amazing UIs.

  • Reusability: Components are reusable, which means that you can use the same components in different parts of your application. This not only saves a significant amount of coding time but also helps to maintain your code.

  • The Virtual DOM (Document Object Model): React creates an in-memory data structure cache, which computes the changes made and then updates the browser. This allows a special feature that lets the programmer code as if the whole page is rendered on each change, while the React libraries only render subcomponents that change.

  • Performance: It improves performance through virtual DOM as real DOM manipulations are expensive.

  • Ecosystem: React has a rich ecosystem, which includes tools like Redux and React Router, that can help you solve complex problems associated with application state management.

  • Community and Backing: It has a massive community, and it's maintained by Facebook.

3. React Basics

To use React, you need to have a basic understanding of JavaScript ES6 features, such as classes, arrow functions, destructuring, and others.

React has two types of components: Functional Components and Class Components. Functional components are simply JavaScript functions and are typically used for UI rendering. Class components are regular JavaScript classes and can handle the state within the component.

Let's take a look at how to create a simple React component:

Functional Component

import React from 'react';

function HelloWorld() {
    return <h1>Hello, world!</h1>;
}

export default HelloWorld;
Enter fullscreen mode Exit fullscreen mode

Class Component

import React, { Component } from 'react';

class HelloWorld extends Component {
    render() {
        return <h1>Hello, world!</h1>;
    }
}

export default HelloWorld;
Enter fullscreen mode Exit fullscreen mode

Both of these components will display the same output: "Hello, world!".

4. Your First React Application

Let's build our first React application. We will use Create React App, a tool that sets up a modern web app by running one command.

Step 1: Install Node.js and npm React requires Node.js version 10.16.0 or later, and npm version 5.6 or later. If you haven't installed Node.js or npm, you can download and install them from here: https://nodejs.org/.

Step 2: Install Create React App Create React App is a command-line tool used to create new React.js projects. You can install it using npm:

npm install -g create-react-app
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a New Project Let's create a new project named my-first-react-app:

npx create-react-app my-first-react-app
Enter fullscreen mode Exit fullscreen mode

This might take a few minutes to create a new React project and install its dependencies.

Step 4: Run the Application Navigate to your new project directory and start the application:

cd my-first-react-app
npm start
Enter fullscreen mode Exit fullscreen mode

You should see a new browser window open up with a React logo and a message saying "Edit src/App.js and save to reload."

Step 5: Edit Your First React Component Now, let's edit our first React component. Open the project in your favorite editor, and navigate to src/App.js. You'll see something like this:

import React from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Replace the contents with the following code:

import React from 'react';

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

export default App;
Enter fullscreen mode Exit fullscreen mode

Save the file, and you should see the browser automatically update to display "Hello, world!".

5. Summary

Congratulations, you've built your first React application! You've learned what React is, why it's used, and you've built a simple React application. This is just the tip of the iceberg when it comes to what you can do with React. As you continue to learn and develop your skills, you'll be able to build more complex and impressive applications.

Top comments (0)