DEV Community

sameer pokharel
sameer pokharel

Posted on

Getting Started with React: A Beginner’s Guide

React is a powerful JavaScript library for building user interfaces, particularly single-page applications requiring a fast, interactive experience. Developed and maintained by Facebook, React has become a popular choice among developers due to its component-based architecture, reusable code, and the ability to manage the state of an application efficiently.

If you’re new to React, this guide will help you understand the basics and get started on your first React application.

What is React?

React is a JavaScript library used to create user interfaces (UIs) by breaking them down into components. A component in React is a reusable piece of code that defines how a section of the UI should look and behave. Components can be nested, managed, and handled independently, making complex UIs more manageable.

React uses a virtual DOM (Document Object Model) to enhance performance. The virtual DOM is a lightweight copy of the actual DOM. When a component’s state changes, React updates the virtual DOM first, compares it with a previous version, and only updates the actual DOM with the differences. This process, called reconciliation, makes React applications fast and efficient.

Key Concepts in React

  1. Components: The building blocks of a React application. They can be class-based or functional. Functional components are simple JavaScript functions, while class components are ES6 classes that extend from React.Component.
  2. JSX (JavaScript XML): A syntax extension for JavaScript that looks similar to HTML and is used in React to describe what the UI should look like. JSX makes it easier to write and add HTML in React.
  3. State: An object that holds some information that may change over the component’s lifecycle. State is managed within the component (using useState in functional components or this.state in class components) and can influence what gets rendered.
  4. Props (Properties): Read-only attributes passed from parent to child components. Props allow you to pass data and event handlers to child components, making components reusable and modular.
  5. Lifecycle Methods: Special methods in class components that run at specific points in a component’s life (e.g., mounting, updating, unmounting). Functional components can use React Hooks like useEffect to mimic these lifecycle methods.
  6. Hooks: Functions that let you “hook into” React state and lifecycle features from functional components. Some popular hooks are useState, useEffect, useContext, and useReducer.

Getting Started with React

To get started with React, you need to set up a development environment. Here’s how:

Step 1: Install Node.js and npm

React relies on Node.js, a JavaScript runtime, and npm (Node Package Manager) to manage dependencies. You can download and install Node.js from nodejs.org. npm is included with Node.js.

To check if you have Node.js and npm installed, run the following commands in your terminal:

node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

If these commands return version numbers, you’re good to go.

Step 2: Set Up a React Application Using Create React App

Create React App is an officially supported tool to create a single-page React application. It sets up your development environment so you can use the latest JavaScript features and provides a nice developer experience.

To create a new React application, run the following commands:

npx create-react-app my-app
cd my-app
npm start
Enter fullscreen mode Exit fullscreen mode

npx create-react-app my-app sets up a new React project named "my-app" with all necessary configurations.
cd my-app moves into the project directory.
npm start runs the app in development mode and opens it in your default browser.
You should see a React welcome page in your browser, confirming that your setup is correct.

Step 3: Understand the Project Structure

Here’s a quick overview of the files and folders created by Create React App:

/public: Contains the index.html file, the entry point of your React application.
/src: Contains the JavaScript code for your application. The main files here are:
index.js: The JavaScript entry point, which renders the React application into the DOM.
App.js: The main component of your application.
You’ll mostly work inside the src folder, creating new components, managing state, and handling events.

Step 4: Create Your First Component

Let’s create a simple component to display a welcome message. Open the App.jsx file and modify it:

import React from 'react';

function App() {
  return (
    <div className="App">
      <h1>Welcome to React!</h1>
      <p>This is your first React component.</p>
    </div>
  );
}
export default App;
Enter fullscreen mode Exit fullscreen mode

This code defines a functional component called App that returns some JSX. Save the file and check your browser. You should see your welcome message.

Step 5: Add More Components

To understand how components work, let’s add another component. Create a new file named Greeting.jsx in the src folder and add the following code:

import React from 'react';

function Greeting(props) {
  return <h2>Hello, {props.name}!</h2>;
}

export default Greeting;
Enter fullscreen mode Exit fullscreen mode

Now, modify App.jsx to include the Greeting component:

import React from 'react';
import Greeting from './Greeting';

function App() {
  return (
    <div className="App">
      <h1>Welcome to React!</h1>
      <p>This is your first React component.</p>
      <Greeting name="John" />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Save both files and check your browser. You should now see “Hello, John!” beneath your welcome message.

Step 6: Use State and Handle Events

Let’s add a button that changes the greeting message when clicked. Modify the Greeting component to use state:

import React, { useState } from 'react';

function Greeting(props) {
  const [message, setMessage] = useState('Hello');
  const handleClick = () => {
    setMessage('Hi');
  };
  return (
    <div>
      <h2>{message}, {props.name}!</h2>
      <button onClick={handleClick}>Change Greeting</button>
    </div>
  );
}

export default Greeting;
Enter fullscreen mode Exit fullscreen mode

This code introduces a message state with a default value of "Hello." When the button is clicked, the handleClick function updates the state to "Hi," causing the component to re-render with the new message.

Conclusion

Congratulations! You’ve just built your first React application and learned the basics of React components, state management, and event handling. From here, you can explore more advanced concepts like context, routing, and Redux for state management.

React’s flexibility and simplicity make it a powerful tool for building modern web applications. Keep experimenting, building, and learning, and soon you’ll be creating complex, interactive UIs with ease.

Happy coding!

Top comments (0)