Optimizing Component Performance in React

๐˜™๐˜ฆ๐˜ข๐˜ค๐˜ต.๐˜ฎ๐˜ฆ๐˜ฎ๐˜ฐ is a higher-order function (HOC) in React that helps optimize the performance of functional components by preventing unnecessary re-renders. It memorizes the result of the component’s rendering and only re-renders it if the ๐ฉ๐ซ๐จ๐ฉ๐ฌ change. Thi…


This content originally appeared on DEV Community and was authored by Ricardo Maia

๐˜™๐˜ฆ๐˜ข๐˜ค๐˜ต.๐˜ฎ๐˜ฆ๐˜ฎ๐˜ฐ is a higher-order function (HOC) in React that helps optimize the performance of functional components by preventing unnecessary re-renders. It memorizes the result of the component's rendering and only re-renders it if the ๐ฉ๐ซ๐จ๐ฉ๐ฌ change. This is useful when a component's props or state don't change often.

๐‡๐จ๐ฐ ๐‘๐ž๐š๐œ๐ญ.๐ฆ๐ž๐ฆ๐จ ๐–๐จ๐ซ๐ค๐ฌ

When a functional component is wrapped in ๐˜™๐˜ฆ๐˜ข๐˜ค๐˜ต.๐˜ฎ๐˜ฆ๐˜ฎ๐˜ฐ, React performs a shallow comparison between the previous and new props. If the props haven't changed, the component won't re-render, saving performance resources by avoiding redundant rendering.

๐๐š๐ฌ๐ข๐œ ๐’๐ฒ๐ง๐ญ๐š๐ฑ
Image description

๐’๐ข๐ฆ๐ฉ๐ฅ๐ž ๐„๐ฑ๐š๐ฆ๐ฉ๐ฅ๐ž
Imagine we have a Counter component and a child component that displays a static message. We can use React.memo for the child component to avoid re-rendering it every time the counter state changes, as long as its props remain the same.

๐–๐ข๐ญ๐ก๐จ๐ฎ๐ญ ๐‘๐ž๐š๐œ๐ญ.๐ฆ๐ž๐ฆ๐จ:

Image description

In this example, every time the counter state changes, ๐˜š๐˜ต๐˜ข๐˜ต๐˜ช๐˜ค๐˜”๐˜ฆ๐˜ด๐˜ด๐˜ข๐˜จ๐˜ฆ will re-render, even though its content remains unchanged.

๐–๐ข๐ญ๐ก ๐‘๐ž๐š๐œ๐ญ.๐ฆ๐ž๐ฆ๐จ:

Image description

Now, with ๐˜™๐˜ฆ๐˜ข๐˜ค๐˜ต.๐˜ฎ๐˜ฆ๐˜ฎ๐˜ฐ, the ๐˜š๐˜ต๐˜ข๐˜ต๐˜ช๐˜ค๐˜”๐˜ฆ๐˜ด๐˜ด๐˜ข๐˜จ๐˜ฆ component will only render once, unless its props change. Even if the counter updates, the static component will not be re-rendered.

๐‘๐ž๐š๐œ๐ญ.๐ฆ๐ž๐ฆ๐จ ๐ฐ๐ข๐ญ๐ก ๐‚๐ฎ๐ฌ๐ญ๐จ๐ฆ ๐‚๐จ๐ฆ๐ฉ๐š๐ซ๐ข๐ฌ๐จ๐ง

By default, ๐˜™๐˜ฆ๐˜ข๐˜ค๐˜ต.๐˜ฎ๐˜ฆ๐˜ฎ๐˜ฐ performs a shallow comparison of props. However, you can provide a custom comparison function if you need finer control over the re-rendering process.

๐„๐ฑ๐š๐ฆ๐ฉ๐ฅ๐ž ๐ฐ๐ข๐ญ๐ก ๐‚๐ฎ๐ฌ๐ญ๐จ๐ฆ ๐‚๐จ๐ฆ๐ฉ๐š๐ซ๐ข๐ฌ๐จ๐ง:

Image description

In this example, the compareProps function checks whether the value prop has changed. If it hasn't, the component won't re-render, even if other parts of the parent component trigger a re-render.

๐‚๐จ๐ง๐œ๐ฅ๐ฎ๐ฌ๐ข๐จ๐ง

๐˜™๐˜ฆ๐˜ข๐˜ค๐˜ต.๐˜ฎ๐˜ฆ๐˜ฎ๐˜ฐ is a simple yet effective tool for optimizing functional components, especially in cases where rendering is costly or the props change infrequently. By using ๐˜™๐˜ฆ๐˜ข๐˜ค๐˜ต.๐˜ฎ๐˜ฆ๐˜ฎ๐˜ฐ, you can improve application performance by reducing unnecessary re-renders.


This content originally appeared on DEV Community and was authored by Ricardo Maia


Print Share Comment Cite Upload Translate Updates
APA

Ricardo Maia | Sciencx (2024-10-02T14:25:50+00:00) Optimizing Component Performance in React. Retrieved from https://www.scien.cx/2024/10/02/optimizing-component-performance-in-react/

MLA
" » Optimizing Component Performance in React." Ricardo Maia | Sciencx - Wednesday October 2, 2024, https://www.scien.cx/2024/10/02/optimizing-component-performance-in-react/
HARVARD
Ricardo Maia | Sciencx Wednesday October 2, 2024 » Optimizing Component Performance in React., viewed ,<https://www.scien.cx/2024/10/02/optimizing-component-performance-in-react/>
VANCOUVER
Ricardo Maia | Sciencx - » Optimizing Component Performance in React. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/02/optimizing-component-performance-in-react/
CHICAGO
" » Optimizing Component Performance in React." Ricardo Maia | Sciencx - Accessed . https://www.scien.cx/2024/10/02/optimizing-component-performance-in-react/
IEEE
" » Optimizing Component Performance in React." Ricardo Maia | Sciencx [Online]. Available: https://www.scien.cx/2024/10/02/optimizing-component-performance-in-react/. [Accessed: ]
rf:citation
» Optimizing Component Performance in React | Ricardo Maia | Sciencx | https://www.scien.cx/2024/10/02/optimizing-component-performance-in-react/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.