Friday, February 9, 2024

Advanced React Hacks 4/10 - Prevent Rerenders

Advanced React Hacks 4/10 - Prevent Rerenders


Very obvious when you see the answer (and actually documented on react.dev, link later) but still worth mentioning for posterity.

You don't want to render too much, because one point of React is that it minimises the amount of rerenders on a screen (or is supposed to)


How do most working React developers see this? With the "highlight updates" tool of course


But, it is worth mentioning (in case it happens again, and for a lesson) that in 2019 with React Developer Tools v4, this killer feature was removed


To the React Team's credit, they restored "Highlight Updates" quickly

Anyway how do you prevent re-rendering? It's actually easy

Once the component has been initially rendered, you can trigger further renders by updating its state with the set function. Updating your component’s state automatically queues a render. (You can imagine these as a restaurant guest ordering tea, dessert, and all sorts of things after putting in their first order, depending on the state of their thirst or hunger.) React.Dev "Managing State"

If you see too much of your screen rendering making it slow when taking actions (like clicking) break it down



Make more components... obvious, but worth mentioning the obvious

Until next time...


3 comments:

  1. This is a useful and practical explanation of React rerendering, especially because it connects a common performance concern with the everyday experience of working with React applications. The discussion around Highlight Updates also adds valuable historical context, showing how developer tooling can make rendering behavior much easier to understand and troubleshoot.

    I particularly liked the emphasis on recognizing when too much of the UI is rendering and approaching the problem by breaking the interface into smaller components. For developers improving their frontend skills, understanding component boundaries and state-driven rendering is an important part of writing efficient applications, and learning through a practical ReactJS Course can reinforce these concepts effectively.

    ReplyDelete
  2. The recommendation to make more components may sound obvious, but it highlights an important React design principle: isolating state and responsibilities can prevent unnecessary parts of the interface from being affected by updates. This approach is also valuable when building mobile interfaces with React-based technologies, where thoughtful component structure can improve maintainability and responsiveness, making a React Native Course a useful next step for developers interested in extending these skills.

    ReplyDelete
  3. Overall, the article does a good job of turning a seemingly simple optimization into a practical lesson about component design, state management, and performance awareness. The focus on observing rerenders before deciding how to optimize is particularly helpful, because developers can apply the same reasoning when developing larger applications and exploring ideas for ReactJS Projects.

    ReplyDelete