Getting Started with React: A Comprehensive Guide

Learn the fundamentals of React and start building modern web applications with this comprehensive guide.

By Santhor Team

What is React?

React is an open-source JavaScript library created by Facebook (now Meta) for building user interfaces, particularly web applications. It allows you to create reusable UI components and build complex, interactive applications with a component-based architecture.

Why React?

  • Component-Based: Build encapsulated components that manage their own state and compose them to create complex UIs
  • Declarative: React makes it painless to create interactive UIs by describing what you want, not how to do it
  • Virtual DOM: React uses a virtual DOM to efficiently update only what changed, resulting in better performance
  • Ecosystem: Large ecosystem of libraries, tools, and community support
  • Learn Once, Write Anywhere: You can develop new features without rewriting existing code, and React skills transfer to React Native for mobile development

Installation and Setup

To get started with React, you have several options:

  1. Create React App (CRA): The traditional way to bootstrap a React app. Run npx create-react-app my-app then cd my-app and npm start.
  2. Vite: A fast, modern build tool. Run npm create vite@latest my-app -- --template react, then cd my-app, npm install, and npm run dev.
  3. Next.js: A React framework for production with features like server-side rendering and routing built-in.

For this guide, we'll use Vite as it provides faster development experience and better performance.

Understanding JSX

JSX (JavaScript XML) is a syntax extension that lets you write HTML-like code in JavaScript. It makes React components more readable:

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

JSX expressions are converted to React.createElement() calls at build time. You can embed JavaScript expressions in JSX using curly braces:

const name = "React";
const element = <h1>Hello, {name}!</h1>;

Your First Component

React components are JavaScript functions that return JSX. Here's a simple component:

function Welcome() {
  return <h1>Hello, World!</h1>;
}

export default Welcome;

Or using arrow function syntax:

const Welcome = () => {
  return <h1>Hello, World!</h1>;
};

Props: Passing Data to Components

Props (short for properties) allow you to pass data from parent components to child components:

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

function App() {
  return (
    <div>
      <Greeting name="Alice" />
      <Greeting name="Bob" />
    </div>
  );
}

With destructuring, you can make it cleaner:

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

State: Making Components Interactive

State allows components to manage and update data over time. With React Hooks (introduced in React 16.8), you use useState:

import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

The useState hook returns an array with two elements: the current state value and a function to update it.

Event Handling

React events are named using camelCase, and you pass functions as event handlers:

function Button() {
  const handleClick = () => {
    alert('Button clicked!');
  };

  return (
    <button onClick={handleClick}>
      Click me
    </button>
  );
}

Conditional Rendering

You can render different content based on conditions:

function Greeting({ isLoggedIn }) {
  if (isLoggedIn) {
    return <h1>Welcome back!</h1>;
  }
  return <h1>Please sign in.</h1>;
}

// Or using ternary operator
function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign in.</h1>}
    </div>
  );
}

Lists and Keys

Render lists of elements using the map() function and provide a unique key prop:

function TodoList({ items }) {
  return (
    <ul>
      {items.map(item => (
        <li key={item.id}>{item.text}</li>
      ))}
    </ul>
  );
}

Essential React Hooks

  • useState: Manage component state
  • useEffect: Handle side effects (API calls, subscriptions, DOM manipulation)
  • useContext: Access context values without prop drilling
  • useRef: Access DOM elements or store mutable values
  • useMemo: Memoize expensive calculations
  • useCallback: Memoize functions

Best Practices

  • Keep components small and focused: Each component should have a single responsibility
  • Use functional components: Prefer function components over class components
  • Lift state up: Share state between components by lifting it to a common ancestor
  • Use meaningful names: Name components and props clearly
  • Extract reusable logic: Create custom hooks for shared functionality

Next Steps

  • Learn about React Router for navigation between pages
  • Explore Context API for global state management
  • Consider state management libraries like Redux or Zustand for complex applications
  • Study React Testing Library for testing your components
  • Read the official React documentation
  • Practice by building small projects like a todo app or a weather dashboard

Conclusion

React is a powerful and flexible library for building modern web applications. Start with the fundamentals—components, props, and state—then gradually explore more advanced concepts like hooks, context, and routing. Build small projects to practice, and don't hesitate to refer to the excellent React documentation as you learn.

More articles · Contact Santhor