Programming

React Basics: Understanding Components and State

Learn the fundamentals of React, including components, state management, and the virtual DOM.

Emjay Sepahi
January 20, 2024
React
JavaScript
Frontend
Components
State

React Basics: Understanding Components and State

React has revolutionized the way we build user interfaces on the web. In this post, we'll explore the core concepts that make React so powerful and popular among developers.

What is React?

React is a JavaScript library for building user interfaces, particularly single-page applications. It was developed by Facebook (now Meta) and is known for its component-based architecture and efficient rendering through the Virtual DOM.

Core Concepts

1. Components

Components are the building blocks of React applications. They are reusable pieces of UI that can contain their own logic and styling.

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

// Class Component
class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}!</h1>;
  }
}

2. Props

Props (properties) are how components receive data from their parent components. They are read-only and help make components reusable.

function UserCard(props) {
  return (
    <div className="user-card">
      <h2>{props.name}</h2>
      <p>{props.email}</p>
      <span>{props.role}</span>
    </div>
  );
}

// Usage
<UserCard name="John Doe" email="john@example.com" role="Developer" />

3. State

State is the internal data of a component that can change over time. When state changes, React re-renders the component.

import { useState } from 'react';

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>
        Increment
      </button>
      <button onClick={() => setCount(count - 1)}>
        Decrement
      </button>
    </div>
  );
}

The Virtual DOM

React uses a Virtual DOM to optimize rendering performance. Here's how it works:

  1. State Change: When component state changes
  2. Virtual DOM Update: React updates the Virtual DOM
  3. Diffing: React compares Virtual DOM with actual DOM
  4. Minimal Updates: Only necessary changes are applied to the real DOM

Hooks

Hooks are functions that allow you to use state and other React features in functional components.

useState Hook

import { useState } from 'react';

function Form() {
  const [name, setName] = useState('');
  const [email, setEmail] = useState('');

  const handleSubmit = (e) => {
    e.preventDefault();
    console.log('Name:', name, 'Email:', email);
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        value={name}
        onChange={(e) => setName(e.target.value)}
        placeholder="Name"
      />
      <input
        type="email"
        value={email}
        onChange={(e) => setEmail(e.target.value)}
        placeholder="Email"
      />
      <button type="submit">Submit</button>
    </form>
  );
}

useEffect Hook

import { useState, useEffect } from 'react';

function UserProfile({ userId }) {
  const [user, setUser] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchUser = async () => {
      try {
        const response = await fetch(`/api/users/${userId}`);
        const userData = await response.json();
        setUser(userData);
      } catch (error) {
        console.error('Error fetching user:', error);
      } finally {
        setLoading(false);
      }
    };

    fetchUser();
  }, [userId]);

  if (loading) return <div>Loading...</div>;
  if (!user) return <div>User not found</div>;

  return (
    <div>
      <h1>{user.name}</h1>
      <p>{user.email}</p>
    </div>
  );
}

Best Practices

1. Component Structure

  • Keep components small and focused
  • Use descriptive names
  • Separate concerns (UI, logic, data)

2. State Management

  • Lift state up when needed
  • Use local state for component-specific data
  • Consider context or Redux for global state

3. Performance

  • Use React.memo for expensive components
  • Implement proper key props in lists
  • Avoid unnecessary re-renders

Common Patterns

Conditional Rendering

function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? (
        <h1>Welcome back!</h1>
      ) : (
        <h1>Please log in.</h1>
      )}
    </div>
  );
}

List Rendering

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

Getting Started

To start a new React project:

# Using Create React App
npx create-react-app my-app
cd my-app
npm start

# Using Vite (recommended)
npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run dev

Next Steps

Once you're comfortable with the basics:

  1. Learn about Context API for state management
  2. Explore React Router for navigation
  3. Study custom hooks for reusable logic
  4. Practice with real projects to solidify concepts

Conclusion

React's component-based architecture and powerful features make it an excellent choice for building modern web applications. Start with these fundamentals, practice regularly, and you'll be building amazing user interfaces in no time!


Ready to dive deeper into React? Check out our advanced React patterns post coming soon!