
Why React Components Re-render (It's Not Just Props)
- كاتب موثّق
- React 19
محتويات المقال
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
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
Trigger 1: the component's own state changes
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
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.
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> );}import { useState } from "react";// The draft lives in the only component that reads itfunction Composer() { const [draft, setDraft] = useState(""); return ( <> <textarea value={draft} onChange={(e) => setDraft(e.target.value)} /> <button onClick={() => send(draft)}>Send</button> </> );}export function ChatRoom({ messages }: { messages: Message[] }) { return ( <section> <MessageList messages={messages} /> <Composer /> </section> );}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.
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.
memo stop it? | ||
|---|---|---|
How to apply it in your project
Find the component that actually changed Record 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. Move state down to where it's read If 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. Pass static content as children Let wrappers that hold state, like modals, tabs and collapsibles, take their content as children, so their state changes never reach it.Keep context values stable Hoist 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.Reach for memo last Once the structure is right, add memowhere 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. memoadds 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 withmemo.
التعليقات (0)