Skip to content

More explicit explanation regarding view updates caused by array mutation #2451

@kamerat

Description

@kamerat

In the list rendering docs, I find this mutation methods being a bit unclear:

Mutation Methods

Vue wraps an observed array's mutation methods so they will also trigger view updates. The wrapped methods are:

  • push()
  • pop()
  • shift()
  • unshift()
  • splice()
  • sort()
  • reverse()

You can open the console and play with the previous examples' items array by calling their mutation methods. For example: example1.items.push({ message: 'Baz' }).

Personally, I find this to be a bit oversimplified in comparison to what it actually means.

I have provided a Codepen example where you can see what this means in practice.

For me, this was somewhat unexpected. I did not until now know that by changing an array which is referenced in dom, Vue will recompute the whole component.*) (* Not necessarily sub-components *)

When I first spotted this in my own codebase, I did not understand how by changing an in data, the whole thing re-computes, for me, I thought logically only the array affected was the only thing to change, but it does make sense now after I have investigated it deeper. However, now that I came back to these docs to see for an explanation, this line is the only thing that is offered:

Vue wraps an observed array's mutation methods so they will also trigger view updates. The wrapped methods are:

Before my observation, reading the docs, I would understood the line as the following:

This is only applied to for-example an v-for: If you update the array used in a v-for, the page(view) will get re-rendered. This seems logical.

Now however, I know that any referenced array that gets mutated by array prototypes will make the page re-render.

My code-pen example above seems pretty obvious to me now, but if you instead of simply rendering the siblingArray, you use it as a prop to a child-component(dom reference), this stuff gets really confusing.

It might be only me being stupid here, but I do think a better more in-depth example/explanation could save more people like me for a headache.

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