Skip to content

"reactive" doesn't work well with Array #219

@lixpng

Description

@lixpng

The reactive can't observe a Array, my code like this:

<template>
  <div>
    <button @click="handleAdd">Add count</button>
  </div>
</template>
<script>
import Vue from "vue";
import { setup, reactive, ref, watch } from "@vue/composition-api";

const observableArray = Vue.observable([{ value: 1 }]);

export default {
  setup() {
    const reactiveArray = reactive([{ value: 1 }]);
    const reactiveObject = reactive({ value: 1 });
    const refArray = ref([{ value: 1 }]);
    const reactiveDeepArray = reactive({parent: [{ value: 1 }]})

    watch(
      () => reactiveArray[0].value,
      val => {
        // I think this should be execute when I click the button every time,
        // but it doesn't
        console.log("reactiveArray changed", val);
      }
    );

    watch(
      () => reactiveObject.value,
      val => {
        // This will execute when I click the button every time
        console.log("reactiveObject changed", val);
      }
    );

    watch(
      () => refArray.value[0].value,
      val => {
        // This will execute when I click the button every time
        console.log("refArray changed", val);
      }
    );

    watch(
      () => observableArray[0].value,
      val => {
        // This will execute when I click the button every time
        console.log("observableArray changed", val);
      }
    );
    
    watch(
      () => reactiveDeepArray.parent[0].value,
      val => {
        // This will execute when I click the button every time
        console.log("reactiveDeepArray changed", val);
      }
    );

    function handleAdd() {
      reactiveArray[0].value++
      reactiveObject.value++
      refArray.value[0].value++
      observableArray[0].value++
      reactiveDeepArray.parent[0].value++
    }

    return {
      handleAdd
    };
  }
};
</script>

So, when I pass a Array to reactive, it won't be observed. But if the Array is wrapped by a object, it can be observed. I think it's a bug. If it's a feature, why?

Metadata

Metadata

Assignees

No one assigned

    Labels

    misalignmentMisalign with the latest RFC

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions