How to Avoid Performance Issues with React's Lift Content Up Pattern

🚀 React Secret: The "Lift Content Up" Pattern (Not State!) 🚀 Everyone talks about lifting STATE up, but almost nobody talks about lifting CONTENT up. This pattern is really useful to avoid performance issue! 🤯 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: → You wrap a component in a provider/context → Now the ENTIRE tree re-renders when context changes → Even components that don't use the context value! → Performance tanks and you don't know why ✨ The Solution: Lift your CONTENT up, keep context consumers small and isolated 🔑 Key Insight: React uses object identity! If you pass JSX as a children prop, React sees it's the same object and skips re-rendering it. So whatever you pass as a children prop will not re-render even if the parent component re-renders. 𝗙𝗼𝗿 𝗺𝗼𝗿𝗲 𝘀𝘂𝗰𝗵 𝘂𝘀𝗲𝗳𝘂𝗹 𝗰𝗼𝗻𝘁𝗲𝗻𝘁, 𝗱𝗼𝗻'𝘁 𝗳𝗼𝗿𝗴𝗲𝘁 𝘁𝗼 𝗳𝗼𝗹𝗹𝗼𝘄 𝗺𝗲. #javascript #reactjs #nextjs #webdevelopment

  • No alternative text description for this image

I actually heard "lifting the content up" for the first time and it really amazes me how we can optimize the performance while using context. Thank you Yogesh for sharing this. However I would like to explicitly comment about the rendering lifecycle of a component present inside context provider. From what I could remember, Taking your example, If Header, Sidebar and Footer don't use the context value (not even extracted the context value), then those component won't be re-rendered if the state changes inside the context provider. Only the MainContent component will be re-rendered since this component uses the context value (value extracted from useContext) even though that value is used in the JSX or not, doesn't matter. Although I still need to verify this, but what do you think about the rendering patterns followed in context?

To view or add a comment, sign in

Explore content categories