Composition-api: Props are not reactive when destructuring-assigned

Created on 9 Oct 2019  路  2Comments  路  Source: vuejs/composition-api

issue

I have been using the composition-api, and found a bug of the setup method.

The code below is reactive. When the props of this component has changed, the name will be outputted to the web console.

import { createComponent, watch } from '@vue/composition-api'

type Props = {
  name: string
}

export default createComponent({
  props: {
    name: {
      type: String
    }
  },
  setup(props: Props) {
    // this is reactive
    watch(() => console.log(props.name))
  }
})

...Although the code below is not reactive.

import { createComponent, watch } from '@vue/composition-api'

type Props = {
  name: string
}

export default createComponent({
  props: {
    name: {
      type: String
    }
  },
  setup({ name }: Props) {
    // not reactive for props
    watch(() => console.log(name))
  }
})

version of @vue/composition-api

^0.3.2

Most helpful comment

Could we have a big warning about this in the docs ? I lost a couple of hours tracking a bug that was caused by destructuring of props.
Or maybe props could be wrapped with toRefs before passing them to the setup function ?

All 2 comments

You should not destructure props because it's a reactive object. Extracting the properties instead of reading them by accessing the key in the object loose reactivity

Could we have a big warning about this in the docs ? I lost a couple of hours tracking a bug that was caused by destructuring of props.
Or maybe props could be wrapped with toRefs before passing them to the setup function ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

swuecho picture swuecho  路  4Comments

spaceemotion picture spaceemotion  路  6Comments

ggedde picture ggedde  路  4Comments

lixpng picture lixpng  路  6Comments

zzachattack2 picture zzachattack2  路  3Comments