Performance: Avoiding prop drilling
MEDIUM IMPACT
This affects interaction responsiveness and rendering speed by reducing unnecessary component re-renders and deep prop passing.
const CountContext = React.createContext(); function Parent() { const [count, setCount] = React.useState(0); return ( <CountContext.Provider value={{ count, setCount }}> <Child1 /> </CountContext.Provider> ); } function Child1() { return <Child2 />; } function Child2() { return <Child3 />; } function Child3() { const { count, setCount } = React.useContext(CountContext); return <button onClick={() => setCount(count + 1)}>{count}</button>; }
function Parent() { const [count, setCount] = React.useState(0); return <Child1 count={count} setCount={setCount} />; } function Child1({ count, setCount }) { return <Child2 count={count} setCount={setCount} />; } function Child2({ count, setCount }) { return <Child3 count={count} setCount={setCount} />; } function Child3({ count, setCount }) { return <button onClick={() => setCount(count + 1)}>{count}</button>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Prop drilling through many components | No extra DOM nodes | Multiple re-renders of intermediate components | Increased paint due to re-renders | [X] Bad |
| Using React Context to share data | No extra DOM nodes | Re-renders only consuming components | Reduced paint cost | [OK] Good |