Native DOM events cause child component rerender, though props haven’t changed #14025
Replies: 2 comments 3 replies
-
|
It's caused by the use of an inline event handler for a native event. Every time the parent re-renders a new function instance is created, which because of the fall-through behaviour triggers the re-rendering of the child component just like a prop change would. You can workaround it by defining the handler in the <script> section instead of in the and using data attributes so you can identify which row triggered the handler with event.currentTarget. |
Beta Was this translation helpful? Give feedback.
-
|
That's what I'm thinking too, but is it mentioned anywhere in the Vue docs? It seems a bit implicit. I guess native handlers are heavily used for situations like this, since there's no reason to define click as an emit. Of course, v-memo fixes the issue, but it's still more of a workaround than a proper solution. @yyx990803 @antfu would appreciate your thoughts on this. Thanks |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone!
I noticed some interesting behavior when Vue re-renders a component template when a native event fires, even if its props haven’t changed.
For example, I have a table component with a child row component that updates active row (activePerson variable) on click. If I attach a native click event to the row component, Vue re-renders its template even when isActive prop stays the same, onUpdated hook is also triggered.
However, if I define click as a custom emitted event (so it becomes a Vue event instead of a native one) inside the row component, the template does not re-render.
My theory is that since native events act like fall-through attributes, they might cause Vue to update a snapshot of the virtual DOM, triggering a re-render as a side effect.
I might be wrong so I’d really appreciate any insight into why this happens — or if there’s any documentation that explains this behavior (I couldn’t find anything relevant).
p.s I know this can be also fixed with v-memo, but I'm curios about the native events specifically.
Thanks!
Stackblitz
https://stackblitz.com/edit/vitejs-vite-kjvz4tn3?file=src%2Fcomponents%2FPeopleTable.vue
Behavior without emit

Behavior with emit

Beta Was this translation helpful? Give feedback.
All reactions