# Special Attributes

# key

  • Expects: number | string | symbol

    The key special attribute is primarily used as a hint for Vue's virtual DOM algorithm to identify VNodes when comparing the new list of nodes against the old list. Without keys, Vue uses an algorithm that minimizes element movement and tries to patch/reuse elements of the same type in-place as much as possible. With keys, it will reorder elements based on the order change of keys, and elements with keys that are no longer present will always be removed/destroyed.

    Children of the same common parent must have unique keys. Duplicate keys will cause render errors.

    The most common use case is combined with v-for:

    <ul>
      <li v-for="item in items" :key="item.id">...</li>
    </ul>
    
    1
    2
    3

    It can also be used to force replacement of an element/component instead of reusing it. This can be useful when you want to:

    • Properly trigger lifecycle hooks of a component
    • Trigger transitions

    For example:

    <transition>
      <span :key="text">{{ text }}</span>
    </transition>
    
    1
    2
    3

    When text changes, the <span> will always be replaced instead of patched, so a transition will be triggered.

# ref

  • Expects: string | Function

    ref is used to register a reference to an element or a child component. The reference will be registered under the parent component's $refs object. If used on a plain DOM element, the reference will be that element; if used on a child component, the reference will be component instance:

    <!-- vm.$refs.p will be the DOM node -->
    <p ref="p">hello</p>
    
    <!-- vm.$refs.child will be the child component instance -->
    <child-component ref="child"></child-component>
    
    <!-- When bound dynamically, we can define ref as a callback function, passing the element or component instance explicitly -->
    <child-component :ref="(el) => child = el"></child-component>
    
    1
    2
    3
    4
    5
    6
    7
    8

    An important note about the ref registration timing: because the refs themselves are created as a result of the render function, you cannot access them on the initial render - they don't exist yet! $refs is also non-reactive, therefore you should not attempt to use it in templates for data-binding.

  • See also: Child Component Refs

# is