الانتقال إلى المحتوى
FrontendReact· 8 دقائق للقراءة

Why React Components Re-render (It's Not Just Props)

Own state, an ancestor's render and context start a render. Changed props are a result, not a cause.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • React 19
محتويات المقال

The support team reports that typing in the chat box feels sluggish. The React DevTools Profiler points at MessageList, which draws a few hundred messages and renders again on every keystroke. The developer is puzzled: MessageList takes exactly one prop, messages, and it's the same array the whole time. "Its props didn't change, so why is it rendering?" The question has the model backwards, and fixing the model fixes the lag.

The common misconception

The belief goes like this: a component is a function of its props, so React re-renders it only when its props change. Same props, no render.

It's a reasonable thing to believe. Most of us learned React as "the UI is a function of props and state", which sounds like React checks the inputs before calling the function. memo really does work that way, so it's easy to assume it's the default. And it's half true: a child that receives new props has indeed just rendered. The mistake is the direction of cause and effect. The new props didn't cause that render; the parent's render did, and it would have rendered the child with the old props too.

What actually happens

React's docs list two reasons for a render: the initial render, and a state update in the component or in one of its ancestors. Context adds a third path. Props aren't on the list. The previous article in this path covered what a render does; this one is about what starts one.

Trigger 1: the component's own state changes

A set function from useState or a dispatch from useReducer schedules a render of that component. State inside a custom Hook counts too: it's still state of the component that calls the Hook. There is one shortcut. If the new value is identical to the current one under Object.is, React skips re-rendering the component and its children. It may still call your component once before bailing out, but it throws that result away.

Trigger 2: an ancestor re-renders

When a component renders, React renders every component it returns, recursively, without comparing props first. The memo docs put it plainly: React normally re-renders a component whenever its parent re-renders. In the chat room, each keystroke updates draft in ChatRoom, so everything ChatRoom returns renders again, MessageList included.

That's all "props changed" ever means. A component gets new props only when its parent renders, and when its parent renders, the child renders anyway. Props are a consequence of the trigger, never the trigger itself.

TSX ChatRoom.tsx
import { useState } from "react";export function ChatRoom({ messages }: { messages: Message[] }) {  const [draft, setDraft] = useState("");  return (    <section>      {/* Same array every time, yet it renders on every keystroke */}      <MessageList messages={messages} />      <textarea value={draft} onChange={(e) => setDraft(e.target.value)} />      <button onClick={() => send(draft)}>Send</button>    </section>  );}

In the first version, draft lives in ChatRoom, so typing re-renders the whole room. In the second, the draft lives in Composer, the only component that reads it. A keystroke renders Composer and nothing above or beside it, with no memo anywhere. React's docs recommend exactly this: prefer local state, and don't lift state further up than you need.

Trigger 3: a context it reads changes

useContext ties a component to the nearest provider above it. When that provider receives a value that differs under Object.is, React re-renders every component reading that context, starting from the provider, and memo doesn't stop it. The catch is the word "differs": an object literal written inline is a new object on every render.

TSX Header.tsx
import { createContext, memo, useContext, useState } from "react";const PrefsContext = createContext({ compact: false });const Header = memo(function Header({ title }: { title: string }) {  const prefs = useContext(PrefsContext);  console.log("Header render"); // logs on every keystroke, memo or not  return <h1 className={prefs.compact ? "small" : "large"}>{title}</h1>;});export function ChatRoom() {  const [draft, setDraft] = useState("");  return (    // A new object on every render: Object.is reports a changed context    <PrefsContext value={{ compact: true }}>      <Header title="Support" />      <textarea value={draft} onChange={(e) => setDraft(e.target.value)} />    </PrefsContext>  );}

Header gets the same title every time, so memo alone would skip it. But each keystroke re-renders ChatRoom, which builds a fresh { compact: true } for the provider, and React sees a changed context. Move that object to a module-level constant, or wrap it in useMemo when it depends on state, and Header stays quiet.

Where props finally matter: memo

memo is React's for components, and the one place where props get compared. A memoized component skips a render caused by its parent when every prop is Object.is-equal to the previous one. It still renders for its own state and for context, and a new object, array or function prop on every render defeats the comparison. The docs call it a performance optimization, not a guarantee. With React Compiler enabled, the compiler applies the equivalent of memo to every component for you.

What happenedDoes it re-render?Does memo stop it?
Its own state was set to a new valueYesNo
Its own state was set to the same valueNo, React bails outNot needed
An ancestor re-rendered, props identicalYesYes
An ancestor re-rendered with a new object or function propYesNo, that prop changed
A context it reads got a new valueYesNo

How to apply it in your project

  1. Find the component that actually changedRecord the slow interaction in the React DevTools Profiler. For each component that rendered, walk up the tree to the nearest one whose state or context changed: that's the trigger, and everything below it rendered along with it.
  2. Move state down to where it's readIf one small part of a big component reads a piece of state, extract that part and give it the state. Drafts, hover flags and open/closed toggles rarely belong at the top of a page.
  3. Pass static content as childrenLet wrappers that hold state, like modals, tabs and collapsibles, take their content as children, so their state changes never reach it.
  4. Keep context values stableHoist constant values out of the component and wrap values built from state in useMemo, so the provider gets a new value only when the data really changed.
  5. Reach for memo lastOnce the structure is right, add memo where the Profiler still shows an expensive component rendering with the same props, or let React Compiler do it.

Key takeaways

  • A component renders for three reasons: its own state changed, an ancestor rendered, or a context it reads changed.
  • Props aren't a trigger: they change only when the parent renders, and by default the child renders then anyway.
  • memo adds the props check, but own state, context and new object or function props all get through it.
  • Fix extra renders with structure first (local state, children, stable context values), then with memo.

📚 المصادر

  1. Render and Commit — React
  2. memo — React API Reference
  3. useContext — React API Reference
  4. useState — React API Reference

التعليقات (0)