Skip to content

Optional props declaration #25

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

Merged
merged 4 commits into from
Nov 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions active-rfcs/0010-optional-props-declaration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
- Start Date: 2019-04-08
- Target Major Version: 2.x & 3.x
- Reference Issues: N/A
- Implementation PR: N/A

# Summary

Make component `props` declaration optional.

# Basic example

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

<script>
export default {}
</script>
```

# 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.

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

<script>
export default {}
</script>
```

If the component has no other options, the entire `<script>` block can be dropped, so the following would be a valid SFC:

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

## Functional Components

This is based on plain-function functional components proposed in [Functional and Async Component API Change](https://github.com/vuejs/rfcs/pull/27).

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

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

``` js
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`):

``` js
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](https://github.com/vuejs/rfcs/pull/28).

# Drawbacks

N/A

# Alternatives

N/A

# Adoption strategy

The behavior is fully backwards compatible.