You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- SFCs will no longer support `<template functional>` - if you need anything more than a function, just use a normal component.
69
69
70
-
- The function signature has also changed - `h` is now imported globally. Instead of a render context, props and slots and other values are passed in. For more details on how the new arguments can replace 2.x functional render context, see the [Render Function API Change RFC](https://github.com/vuejs/rfcs/pull/28).
70
+
- The function signature has also changed:
71
+
-`h` is now imported globally.
72
+
- The function receives two arguments: `props` and a context object that exposes `slots`, `attrs` and `emit`. These are equivalent to their `$`-prefixed equivalents on a stateful component.
71
73
72
-
## Runtime Props Validation
74
+
## Comparison with Old Syntax
73
75
74
-
Props declaration is now optional (only necessary when runtime validation is needed). To add runtime validation or default values, attach `props` to the function itself:
76
+
The new function arguments should provide the ability to fully replace the [2.x functional render context](https://vuejs.org/v2/guide/render-function.html#Functional-Components):
77
+
78
+
-`props` and `slots` have equivalent values;
79
+
-`data` and `children` are no longer necessary (just use `props` and `slots`);
80
+
-`listeners` will be included in `attrs`;
81
+
-`injections` can be replaced using the new `inject` API (part of [Composition API](https://vue-composition-api-rfc.netlify.com/api.html#provide-inject)):
82
+
83
+
```js
84
+
import { inject } from'vue'
85
+
import { themeSymbol } from'./ThemeProvider'
86
+
87
+
constFunctionalComp=props=> {
88
+
consttheme=inject(themeSymbol)
89
+
returnh('div', `Using theme ${theme}`)
90
+
}
91
+
```
92
+
93
+
-`parent` access will be removed. This was an escape hatch for some internal use cases - in userland code, props and injections should be preferred.
94
+
95
+
## Optional Props Declaration
96
+
97
+
To make it easier to use for simple cases, 3.x functional components do not require `props` to be declared:
98
+
99
+
```js
100
+
constFoo=props=>h('div', props.msg)
101
+
```
102
+
```html
103
+
<Foomsg="hello!" />
104
+
```
105
+
106
+
With no explicit props declaration, the first argument `props` will contain everything passed to the component by the parent.
107
+
108
+
## Explicit Props Declaration
109
+
110
+
To add explicit props declarations, attach `props` to the function itself:
75
111
76
112
```js
77
113
constFunctionalComp=props=> {
@@ -85,27 +121,7 @@ FunctionalComp.props = {
85
121
86
122
## Async Component Creation
87
123
88
-
With the functional component change, Vue's runtime won't be able to tell whether a function is being provided as a functional component or an async component factory. So in v3 async components must now be created via a new API method:
This will make async component creation a little more verbose, but async component creation is typically a low-frequency use case, and are often grouped in the same file (the routing configuration).
124
+
The new async component API is discussed in [its own dedicated RFC](https://github.com/vuejs/rfcs/pull/148).
109
125
110
126
# Drawbacks
111
127
@@ -119,6 +135,4 @@ N/A
119
135
120
136
- For functional components, a compatibility mode can be provided for one-at-a-time migration.
121
137
122
-
- For async components, the migration is straightforward and we can emit warnings when function components return Promise instead of VNodes.
123
-
124
138
- SFCs using `<template functional>` should be converted to normal SFCs.
0 commit comments