‫Let's wrap up the performance optimization part
‫of this section with a quick list of dos and don'ts.
‫So the main message of this video
‫is that you should not optimize prematurely.
‫And what do I mean by that?
‫Well, basically, don't optimize anything
‫if there is nothing to optimize.
‫So, if your app is performing just fine
‫then don't just wrap all your components into a memo
‫or all your values and callbacks
‫into useMemo and useCallback.
‫Also, there is no need to optimize context
‫if it's not slow and if it doesn't have many consumers.
‫So doing all these things
‫can actually do more harm than good
‫because memoization itself
‫can have a slight hit on performance.
‫And so, if you have a thousand useless useMemos
‫it can actually all add up
‫and then even worsen the performance of your application.
‫And that's not to mention
‫that your code is gonna become unreadable and super messy
‫if you wrap everything into a useCallback or a useMemo.
‫So please don't do that.
‫Instead, what you should do
‫is to find actual performance bottlenecks
‫using the profiler tool
‫that we learned about in the section.
‫Or even better, identify problems visually.
‫For example, when you see a laggy and sluggish UI,
‫because then you can focus
‫on those real performance issues and fix them.
‫For example, by memoizing expensive re-renders
‫or expensive calculations.
‫Now when it comes to the context API
‫it only makes sense to optimize re-renders
‫if a certain context has lots of consumers
‫and if the state updates all the time
‫causing a significant issue.
‫So in that case, you can try to memoize the context value,
‫memoize all the direct child components of the provider,
‫or set up two separate contexts.
‫So one for the actual state value
‫and one for updating the state.
‫Finally, it's also super important
‫to always implement code splitting and lazy loading
‫for all the routes in your single page applications.
‫Alright, and with that, you're well on your way
‫to mastering performance optimization in React.
‫It can be a pretty confusing subject
‫but also a very important one.
‫And so, it's great to see that you're still here with me.