FE1: How React really renders

Lesson 1 of 5 · reading 15–25 minutes; practice and project time is on the module page

Render, commit and why components re-render

🎯You'll be able to

Explain when and why a component re-renders, and tell a render from a commit using React Developer Tools.

🏠The everyday picture

A shop updates its price board each morning. The shopkeeper first writes the new prices on a notepad, compares them with the board, then changes only the prices that differ. Nobody repaints the whole board for one changed price.

📘What it really is

📝A worked example

Kirana Express, a grocery ordering app for a neighbourhood store chain in Pune, has this screen:

function ShopPage() {
  const [search, setSearch] = useState('');
  const [cart, setCart] = useState([]);
  return (
    <>
      <SearchBox value={search} onChange={setSearch} />
      <ProductList products={PRODUCTS} search={search} onAdd={p => setCart([...cart, p])} />
      <CartBadge count={cart.length} />
    </>
  );
}

Typing one letter in the search box changes search, so ShopPage re-renders, and so do SearchBox, ProductList and CartBadge. The React DevTools Profiler shows CartBadge re-rendering on every keystroke, although its count didn't change. That's normal and cheap here. It only matters if a component is slow, which Lesson 4 deals with.

🌍Where it's used

📝Case scenario

A developer adds analytics.track('viewed product') directly in a product card's function body. The dashboard shows each product "viewed" three or four times per visit. Rendering can happen many times, so tracking inside render counts renders, not views. Moving the call into an effect takes it out of render, so re-renders stop adding events. An effect still runs again when the card remounts, and twice on purpose in development under Strict Mode, so decide what counts as one "view" (for example, a product id once per page visit) and let the tracking code ignore repeats of the same view.

🖱️Try it

Create the Kirana Express product page above with 200 sample products. Open the React DevTools Profiler, record while typing three letters, and write down which components rendered and how long each took.

⚠️Common mistakes

🛠️For your project

Open the starter Kirana Express app with React Developer Tools, add a product to the cart and note which components re-render. You'll use this when building the cart in Project 2.

✅Quick check

Name three things that make a component re-render.

Show the answer

A change in its own state, its parent re-rendering, or a change in a context it reads.

Why should a component's render be pure?

Show the answer

Because React may render it many times, so side effects inside render would repeat unpredictably.

← BackFE1
ProLearners Assist