Skip to content

Improve the props parameter in setup function #3288

Closed
@Justineo

Description

@Justineo

What problem does this feature solve?

import { defineComponent, toRefs, watch } from "vue";

export default defineComponent({
  props: {
    foo: Boolean
  },
  setup(props) {
    const { foo } = toRefs(props);

    // As `foo` might be undefined instead of a Ref
    // we cannot watch it directly like this
    watch(foo, () => { ... }); 
  }
});

Instead, we need to use:

watch(() => props.foo, () => { ... });

So we need to switch to props.foo whenever we need to watch it, which doesn't feel very natural to me.

What does the proposed API look like?

For all declared props, always provide { [propName]: undefined } in the props parameter of the setup function so that we can write:

setup(props) { // props being { foo: undefined }
  const { foo } = toRefs(props);

  // foo is Ref
  watch(foo, () => { ... }); 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions