Getting Started with React

Begin your journey with React! Learn how to set up your environment, create your first component, and understand the basics every web developer should know.


Back to Home

Table of content

Introduction

React is a popular JavaScript library developed by Facebook for building user interfaces, particularly single-page applications. Thanks to its component-based architecture and efficient rendering, React has become an essential tool for modern web development. If you’re new to React, this guide will help you get started quickly and confidently.

Why Use React?

  • Component-based: Build encapsulated components that manage their own state.
  • Declarative: Design simple views for each state in your application.
  • Large ecosystem and community: Tons of resources, libraries, and support.
  • Fast rendering: The virtual DOM efficiently updates only what’s necessary.

Setting Up the Development Environment

Option 1: Using Create React App

  1. Make sure Node.js and npm are installed. You can check with node -v and npm -v.
  2. Run the following command in your terminal:
    npx create-react-app my-app
  3. Go to your new project folder and start the development server:
    cd my-app
    npm start

Option 2: Using Vite (Faster builds)

  1. Install Vite and create a new React project:
    npm create vite@latest my-app -- --template react
  2. Install dependencies and start the server:
    cd my-app
    npm install
    npm run dev

Option 3: CodeSandbox/StackBlitz

If you want to experiment fast, try online editors like CodeSandbox or StackBlitz. Choose the React template, and you’re ready to code!

Your First React Component

React applications are built from components. Here’s a simple example:

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

// Usage
<Welcome name="World" />

This creates a reusable component that greets a user by name.

Understanding JSX

React components use JSX, a syntax extension that lets you write HTML-like code directly inside JavaScript:

const element = <h1>Hello, React!</h1>;

JSX makes code more readable and expressive, but it’s transpiled to JavaScript under the hood.

Adding State with useState

Components can hold their own state using the useState hook:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

Project Structure Overview

  • src/App.js — Main app component
  • src/index.js — Entry point (renders App to the DOM)
  • public/index.html — The root HTML file

Next Steps

  • Learn about props and component composition
  • Explore advanced hooks: useEffect, useContext
  • Style your components using CSS or styled-components
  • Read the official React documentation

Conclusion

React is a must-learn technology for web development in 2024 and beyond. By mastering the basics—components, state, and JSX—you’ll be well on your way to building modern, responsive web apps. Happy coding!

Beginner
Frontend
JavaScript
React
SPA
Web Development