Vue: Is Vue performing unnecessary re-render when using $listeners?

Created on 16 Dec 2017  路  5Comments  路  Source: vuejs/vue

Version

2.5.11 (and earlier versions)

Reproduction link

https://jsfiddle.net/xb4172g8/

Steps to reproduce

  1. Open console and observe while typing something into input fields
  2. Enter some text into name field -> Vue re-renders all three Textfield components
  3. In line 21 replace { ...this.$listeners } with an empty object
  4. Once again enter some text into any field -> Vue re-renders only updated Textfield

What is expected?

Vue should re-render only the component whose props has changed.

What is actually happening?

Using $listeners in component's render function causes the component to be rendered whenever its parent is updated even though his props hasn't changed.

improvement

Most helpful comment

Unfortunately even if we do a shallowEqual check before setting $listeners, your specific example would still re-render because your @input listeners are created as a new anonymous function for each render, so technically they are different. This is similar to passing down a different anonymous function as prop on every render.

All 5 comments

At the moment we always set $listeners when updating the parent, so yes, it triggers children updates if the use $listeners in the render function. Maybe this could be prevented by making sure the listeners have been updated before setting $listeners (https://github.com/vuejs/vue/blob/dev/src/core/instance/lifecycle.js#L243)

Unfortunately even if we do a shallowEqual check before setting $listeners, your specific example would still re-render because your @input listeners are created as a new anonymous function for each render, so technically they are different. This is similar to passing down a different anonymous function as prop on every render.

Has there been made any plans to implement such a check?

Is there any workaround for this issue? I had to manually set up all listeners on a transparent wrapper, but it doesn't feel right. Actually, how is it going to work in Vue 3?

is only resolve this problem like vuetify?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ecmel picture ecmel  路  52Comments

rpkilby picture rpkilby  路  50Comments

karevn picture karevn  路  42Comments

wenLiangcan picture wenLiangcan  路  39Comments

yyx990803 picture yyx990803  路  36Comments