Skip to content

ref function is called even though DOM node does not change #4084

Closed

Description

@sqal

Version

3.1.4

Reproduction link

https://sfc.vuejs.org/ playground

Steps to reproduce

open console and hit the button a few times

What is expected?

setRef to be called only when element is mounted/unmounted

What is actually happening?

setRef is called each time an update happens, even though the ref function is static and DOM element does not change. I may be wrong here but thought it should work like React's function ref, but maybe it works differently in Vue. If so and this is not a bug, then sorry for asking, and please close the issue. Thanks 👍

Activity

  1. edison1105 commented on Jul 9, 2021

    @edison1105
    Member

    This is expected, setRef will execute on each time patch and unmount.

  2. LinusBorg commented on Jul 9, 2021

    @LinusBorg
    Member

    It's currently implemented this way intentionally, yes I wonder if we can optimize it though. We should be able to compare the ref value for the old and vnode and skip the operation if they have the same value.

    Edit: That's colliding a bit with the pattern of using a ref function on a v-for element, were we recommend resetting an array of template refs onBeforeUpdate so it can be re-populated during rendering ...

    Still, might be something to look into more thoroughly.

  3. yyx990803 commented on Jul 9, 2021

    @yyx990803
    Member

    This is intended behavior. The cost of setting a ref with the same value is negligible and it enables the pattern of constructing fresh ref lists on each render.

  4. locked and limited conversation to collaborators on Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions