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

Optional Props Declaration #26

Closed
yyx990803 opened this issue Mar 21, 2019 · 3 comments
Closed

Optional Props Declaration #26

yyx990803 opened this issue Mar 21, 2019 · 3 comments

Comments

@yyx990803
Copy link
Member

yyx990803 commented Mar 21, 2019

  • Start Date: 2019-03-21
  • Target Major Version: 2.x & 3.x
  • Reference Issues: N/A
  • Implementation PR: N/A

Summary

Make component props declaration optional.

Basic example

<!-- valid SFC -->
<template>
  <div>{{ $props.foo }}</div>
</template>

Motivation

In simple use cases where there is no need for runtime props type checking (especially in functional components), making props optional could result in simpler code.

Detailed design

Stateful Components

When a component has no props declarations, all attributes passed by the parent are exposed in this.$props. Unlike declared props, they will NOT be exposed directly on this. In addition, in this case this.$attrs and this.$props will be pointing to the same object.

Nice thing about this is you can omit the <script> block altogether in a simple SFC:

<template>
  <div>{{ $props.foo }}</div>
</template>

Functional Components

This is based on plain-function functional components proposed in Functional and Async Component API Change.

const FunctionalComp = props => {
  return h('div', props.foo)
}

To declare props for plain-function functional components, attach it to the function itself:

FunctionalComp.props = {
  foo: Number
}

Similar to stateful components, when props are declared, the props arguments will only contain the declared props - attributes received but not declared as props will be in the 3rd argument (attrs):

const FunctionalComp = (props, slots, attrs) => {
  // `attrs` contains all received attributes except declared `foo`
}

FunctionalComp.props = {
  foo: Number
}

For mode details on the new functional component signature, see Render Function API Change.

Drawbacks

N/A

Alternatives

N/A

Adoption strategy

The behavior is fully backwards compatible.

@posva
Copy link
Member

posva commented Mar 23, 2019

Regarding TS users, they won't be able to use this, would they?
About the runtime check, I know it's unrelated, but are we still leaving it for development only?

@yyx990803
Copy link
Member Author

@posva they can, with the <Props> generic argument.

For the runtime check, yes it is still dev only.

@yyx990803
Copy link
Member Author

Published: vuejs/rfcs#25

@github-actions github-actions bot locked and limited conversation to collaborators Nov 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants