DEV Community

Cover image for Read your React components bottom-up (the JSX is the promise)
Nupur for Solitaire Infosystems

Posted on Edited on

Read your React components bottom-up (the JSX is the promise)

Read React Components Bottom-Up: A Simple Habit That Makes Debugging Faster

Most of us read a component the way we read a book: top to bottom.

Imports, then props, then state, then effects, then, 80 lines later, the JSX.

By the time you reach the return, you're holding a pile of variables with no idea what they're for.

At Solitaire Infosystems, a software development company in Mohali, our React and Next.js interns start with reading, not building. The first habit we teach is to read a component bottom-up.

Here's how it works.

The Idea

The JSX at the bottom is the promise: this is what the user will see, and these are the things they can do.

Everything above the return exists only to keep that promise.

So start at the return and work upward. Every time you hit an identifier, ask one question:

Where does this come from?

A Small Example

Let's look at a simple React component:

import { useEffect, useState } from "react";

function ProductList({ products, onSelect }) {
  const [query, setQuery] = useState("");
  const [sortBy, setSortBy] = useState("price");

  const visible = products
    .filter((p) => p.name.toLowerCase().includes(query.toLowerCase()))
    .sort((a, b) => (a[sortBy] > b[sortBy] ? 1 : -1));

  useEffect(() => {
    document.title = `${visible.length} products`;
  }, [visible.length]);

  return (
    <section>
      <input
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        placeholder="Search products"
      />
      <select value={sortBy} onChange={(e) => setSortBy(e.target.value)}>
        <option value="price">Price</option>
        <option value="name">Name</option>
      </select>
      <ul>
        {visible.map((p) => (
          <li key={p.id}>
            <button onClick={() => onSelect(p.id)}>
              {p.name} · ₹{p.price}
            </button>
          </li>
        ))}
      </ul>
    </section>
  );
}
Enter fullscreen mode Exit fullscreen mode

Now Read It Bottom-Up

Start at the return and list what it uses:

  • query and setQuery
  • sortBy and setSortBy
  • visible
  • onSelect
  • The fields id, name, and price on each product

Now trace each one upward:

  • onSelect and products are props. The parent owns them; this component can only read them or call the callback.
  • query and sortBy are state. They're the only things this component remembers, and the user changes them through the two handlers in the JSX.
  • visible is derived. It's computed from products, query, and sortBy on every render and stored nowhere.
  • The effect syncs something outside React. Here, it updates the browser tab title based on what's on screen.

In about 30 seconds, you know what the component renders, what it needs from its parent, what it remembers, and what it does to the outside world.

You never had to read it top-down.

Three Things This Habit Catches

1. State That Should Be Derived

A common beginner pattern is to keep visible in state and update it with an effect whenever query changes.

Reading bottom-up, you see that visible can always be computed from values you already have.

Storing it adds an extra render and gives the two copies a chance to drift apart.

The React documentation makes the same point in You Might Not Need an Effect.

2. Dead Weight

If a variable above the return isn't used by the JSX, a handler, or an effect the JSX triggers, you can probably delete it.

Bottom-up reading finds these in seconds.

3. Components That Do Too Much

If you can't say in one sentence what the JSX promises, the component is probably doing two jobs.

Split it first, then clean up the logic.

Try It on Your Own Code

Pick one component from your current project and read it bottom-up.

Then write one comment above the return:

This component promises ___.

If the sentence needs the word "and" twice, that's your next refactor.

Where This Comes From

We run this as a component walkthrough in the React JS and Next.js track at Solitaire Infosystems.

Interns read real components out loud, bottom-up, before they're allowed to change one.

It sounds slow, but it makes them faster at debugging, reviewing, and getting productive in an unfamiliar codebase.

If you'd like to learn React this way, explore our React JS course in Chandigarh.


How do you read a new component: top-down or bottom-up? Tell us in the comments!

Top comments (0)