Code Reference

Thinking in React

React · Reference cheat sheet

Thinking in React

React · Reference cheat sheet


📋 Overview

“Thinking in React” is a design approach: break the UI into a component hierarchy, decide what is state vs props, and let data flow downward. Build static UI first, then add interactivity.

🔧 Core concepts

StepWhat you do
1. Mock / sketchKnow the screens and pieces
2. Split componentsOne responsibility per component
3. Static versionRender with hard-coded props/data
4. Find stateWhat changes over time? Minimize it
5. Place stateOwn state in the closest common parent
6. Data flowProps down; events up

If two components need the same changing data, lift state to their parent.

💡 Examples

Component split (static):

function SearchBar({ query }) {
  return <input value={query} readOnly />;
}

function ProductList({ products }) {
  return (
    <ul>
      {products.map((p) => (
        <li key={p.id}>{p.name}</li>
      ))}
    </ul>
  );
}

function FilterableList({ products }) {
  return (
    <>
      <SearchBar query="" />
      <ProductList products={products} />
    </>
  );
}

Add state in the parent:

import { useState } from "react";

function FilterableList({ products }) {
  const [query, setQuery] = useState("");
  const filtered = products.filter((p) =>
    p.name.toLowerCase().includes(query.toLowerCase())
  );

  return (
    <>
      <input
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        placeholder="Search"
      />
      <ProductList products={filtered} />
    </>
  );
}

Props down, events up:

function SearchBar({ query, onQueryChange }) {
  return (
    <input
      value={query}
      onChange={(e) => onQueryChange(e.target.value)}
    />
  );
}

⚠️ Pitfalls

  • Duplicating the same state in two places causes them to drift.
  • Derivable values (filtered lists) usually should not be stored as separate state.
  • Giant “god components” are hard to change — split earlier than you think.
  • Don’t fetch in every tiny child — load data near the top or in a dedicated module.

On this page