Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Props are not reactive when destructuring-assigned #156

Closed
yutanoma opened this issue Oct 9, 2019 · 3 comments
Closed

Props are not reactive when destructuring-assigned #156

yutanoma opened this issue Oct 9, 2019 · 3 comments

Comments

@yutanoma
Copy link

yutanoma commented Oct 9, 2019

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

@posva
Copy link
Member

posva commented Oct 9, 2019

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

@posva posva closed this as completed Oct 9, 2019
@salvadordiaz
Copy link

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 ?

@panzerdp
Copy link

I wrote a post about how to correctly destructure the props and not lose the reactivity. You might find it useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants