Subscribe on changes!

when watch Array newValue and oldValue is incorrect

avatar
Oct 18th 2020

Version

3.0.1

Reproduction link

https://jsfiddle.net/v51bpjek/4/

Steps to reproduce

like code bellow

        this.arr = [1, 2, 3, 4, 5];
        this.$watch(
            'arr',
            (n, o) => {
                console.log(n);
                console.log(o);
            },
            {
                deep: true,
            },
        );
        this.arr.push(6)

What is expected?

when arr changed , n !== o like this this.arr.push(6)

What is actually happening?

n===o when use like push splice method

avatar
Oct 18th 2020

This is expected and was like that in Vue 2 as well, and is documented.

Vue's state is not immutable. the whole Reactivity is based on mutating state. So the old array is the same as the new array.

there's no way to compare the old and new state like that.