Skip to content

Should v-model-checkbox mutate its array? #178

Open
@jods4

Description

@jods4

If the v-model of a checkbox is an array, currently Vue creates a new copy of that array for each change and assigns it (if possible...).

This comes with some drawbacks that make me wonder whether mutating the array would be a better idea.

You can play with and try to fix this fiddle to get a feel for the situations where it's not ideal.
https://jsfiddle.net/3g89mswr/7/

The fact that you need to be able to assign is restrictive in some situations. In the fiddle I try to use a prop from a stateless (could be functional) component but props are not assignable.

Not only must the target be assignable but it must also be reactive. Even if you don't intend on reacting to the changes (in the example above, I only read back the array when clicking on a button) a non-reactive property holding this array is buggy because of the way v-model-checkbox work. If the property is not reactive any change will always be applied to the initial array (and previous changes are lost).

One argument could be made that similar approach would also fail for a string prop with v-model on a textbox.
I would agree with that, but I'm not sure if the comparison is really obvious. I intuitively thought the array would mutate and I was surprised when it didn't. Also the text input version would work even with a non-reactive source property.

Using mutations would IMHO make Vue work in more situations and reduce friction.

Activity

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