In Vue.js, the component will re-render whenever a child component’s props change. This simple concept leads us to a crucial best practice that can significantly enhance our app’s performance: prop s...
Gurpreet Kait
Author
Share:
Link copied to clipboard!
In Vue.js, the component will re-render whenever a child component’s props change. This simple concept leads us to a crucial best practice that can significantly enhance our app’s performance: prop stability.
http://localhost/storage/uploads/Single-Responsibility-Principle-In-PHP-1024x576.png alt="prop stability in vue application" class="wp-image-1236"/>
What is Prop Stability?
Prop stability means keeping the props passed to a component stable, so they don't change frequently. This helps to avoid unnecessary re-renders and enhances your app's performance.
Example: List Item Component
Imagine we have a ListItem component that receives id, message, and activeId as props. Here’s how it might look in the template:
When you click the button, you’ll notice that all ListItem components re-render. This happens because the activeId prop changes for every item, causing unnecessary re-renders.
Improving with Stable Props
To optimize this, we can change the ListItem component to use an isActive prop instead of activeId:
With this change, only the active item and the previously active item re-render when activeId changes. This reduces the number of re-renders, improving performance, especially for large lists.
I hope this will help, Happy Coding!
Enjoyed this article?
Subscribe to get more Laravel tutorials and development tips like this one
No spam, ever. Unsubscribe at any time.
My Project
ScreenSense
A Loom alternative built for individuals. Record, share, and explain anything with simple screen recordings.