top of page
Search

Understanding Smart and Dumb Components in React

  • Writer: Marko Reljić
    Marko Reljić
  • Jul 11, 2023
  • 3 min read

React, a popular JavaScript library developed by Facebook, is best known for its robust approach to building user interfaces (UIs). At the core of every React application, we find components. These are self-contained, reusable pieces of code that are responsible for one aspect of the user interface. They work much like JavaScript functions, but they return HTML via a render function.

Components allow developers to break down complex UIs into smaller, more manageable parts. Each part is isolated, which makes the code easier to maintain, understand, and test. It also enables developers to reuse these parts across their application, promoting consistency and reducing the amount of code written.

React components can generally be categorized into two types: Dumb (or presentational) components and Smart (or container) components. Let's dive deeper to understand them.



Dumb Components

Dumb components, also referred to as presentational components, are primarily concerned with how things look. Their main responsibility is to render UI.

A few key characteristics of dumb components include:

  1. They focus on UI: Dumb components are mainly concerned with rendering markup and styles based on the props they receive. They don't dictate how the data is loaded or manipulated; they simply display the data provided to them.

  2. They have no logic: Dumb components are generally functional and do not have their own logic. Their role is to present information and delegate user interaction up to parent components.

  3. They should be reusable: Due to their simplicity and lack of dependencies, dumb components are designed to be highly reusable across your application.

  4. They accept props to be reusable: Props (short for properties) allow data to be passed into components, making them dynamic and reusable. A dumb component's render output is fully determined by its props.

  5. They rarely have their own state: Dumb components rarely maintain their own state. If they do, it's for UI-level concerns, like toggling a dropdown menu or an input field value, rather than data or app state.

Here's a simple example of a dumb component:

function WelcomeMessage({ name }) {
    return <h1>Welcome, {name}!</h1>;
}

This WelcomeMessage component accepts a name prop and returns a simple greeting. It's a dumb component because it's only responsible for rendering UI based on the props it receives.

Smart Components


Smart components, also known as container components, are more sophisticated. They manage data, maintain state, and include the logic for how the data is loaded, manipulated, and passed down to the dumb components.


A few key characteristics of smart components include:

  1. They have multiple responsibilities: Unlike dumb components, smart components often deal with business logic and state management. They're responsible for how things work.

  2. They use hooks, libraries, APIs, etc: Smart components may interact with various hooks, external libraries, or APIs to fetch, manipulate, and store data.

  3. They manage state: They often maintain state that can be shared across multiple dumb components. They act as a container for both the state and the logic to handle user interactions or side effects.

  4. They pass data to dumb components: Smart components often pass data to their child (dumb) components, which then display the data.

  5. They are usually not heavily styled: The focus of smart components is on functionality rather than presentation, so they typically contain little or no styling.

Here's a simple example of a smart component:

import React, { useState, useEffect } from "react";
import axios from "axios";
import WelcomeMessage from "./WelcomeMessage";

function WelcomeContainer() {
    const [name, setName] = useState("");

    useEffect(() => {
        axios.get("/api/user").then((response) => setName(response.data.name));
    }, []);

    return <WelcomeMessage name={name} />;
}

export default WelcomeContainer;

The WelcomeContainer component fetches data from an API, manages it within its own state, and passes it to the WelcomeMessage component as a prop. This makes WelcomeContainer a smart component, as it's handling data and logic, then passing the data to a dumb component for presentation.



In conclusion, smart and dumb components provide a powerful way to structure React applications. By separating concerns between how things look (dumb components) and how things work (smart components), developers can create more readable, maintainable, and scalable code.


 
 
 

Comments


bottom of page