Performance: Why context is needed
MEDIUM IMPACT
This concept affects interaction responsiveness and rendering efficiency when passing data through component trees.
const UserContext = React.createContext(null); function Parent() { const user = { name: 'Alice' }; return <UserContext.Provider value={user}> <Child1 /> </UserContext.Provider>; } function Child3() { const user = React.useContext(UserContext); return <div>{user.name}</div>; }
function Parent() { const user = { name: 'Alice' }; return <Child1 user={user} />; } function Child1({ user }) { return <Child2 user={user} />; } function Child2({ user }) { return <Child3 user={user} />; } function Child3({ user }) { return <div>{user.name}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Prop drilling through many components | Many props passed down | Multiple re-renders | Higher paint cost due to re-renders | [X] Bad |
| Using React Context for shared data | Minimal props passed | Re-renders only consuming components | Lower paint cost | [OK] Good |