Skip to content

Commit a206ac7

Browse files
bencodezenphanan
andauthored
docs (#134): add functional components to migration guide (#135)
* docs (#134): add functional components to migration guide * docs (#134): update overview section for functional components migration * docs: improve phrasing Co-authored-by: Phan An <me@phanan.net> Co-authored-by: Phan An <me@phanan.net>
1 parent 88dd566 commit a206ac7

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/.vuepress/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const sidebar = {
7575
{
7676
title: 'Migration to Vue 3',
7777
collapsable: true,
78-
children: ['migration']
78+
children: ['migration', 'migration/functional-components']
7979
},
8080
{
8181
title: 'Contribute to the Docs',
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Functional Components
2+
3+
## Overview
4+
5+
In terms of what has changed, at a high level:
6+
7+
- Performance gains from v2 for functional components are now negligible in v3, so we recommend just using stateful components
8+
- `functional` attribute on single-file component `<template>` is deprecated
9+
- Functional components can only be created using a plain function that receives `props` and `context` (i.e., `slots`, `attrs`, `emit`)
10+
11+
For a more in-depth explanation, read on!
12+
13+
## Introduction
14+
15+
In Vue 2, functional components had primary use cases:
16+
17+
- as a performance optimization, because they initialized much faster than stateful components
18+
- to return multiple root nodes
19+
20+
However, in Vue 3, the performance of stateful components has improved to the point that the difference is negligible. In addition, stateful components now also include the ability to return multiple root nodes.
21+
22+
As a result, the only remaining use case for functional components is simple components, such as a component to create a dynamic heading. Otherwise, it is recommended to use stateful components as you normally would.
23+
24+
## Previous Syntax
25+
26+
Using the `<dynamic-heading>` component, which is responsible for rendering out the appropriate heading (i.e., `h1`, `h2`, `h3`, etc.), this could have been written as a single-file component in v2 as:
27+
28+
```js
29+
// Vue 2 Functional Component Example
30+
export default {
31+
functional: true,
32+
props: ['level'],
33+
render(h, { props, data, children }) {
34+
return h(`h${props.level}`, data, children)
35+
}
36+
}
37+
```
38+
39+
Or, for those who preferred the `<template>` in a single-file component:
40+
41+
```js
42+
// Vue 2 Functional Component Example with <template>
43+
<template functional>
44+
<component
45+
v-bind:is="`h${props.level}`"
46+
v-bind="attrs"
47+
v-on="listeners"
48+
/>
49+
</template>
50+
51+
<script>
52+
export default {
53+
props: ['level']
54+
}
55+
</script>
56+
57+
```
58+
59+
## Current Syntax
60+
61+
Now in Vue 3, the only way to create a functional component is with a plain function that receives `props` and a `context` object that contains `attrs`, `slots`, and `emit` options:
62+
63+
```js
64+
import { h } from 'vue'
65+
66+
const DynamicHeading = (props, context) => {
67+
return h(`h${level}`, context.attrs, context.slots)
68+
}
69+
70+
DynamicHeading.props = ['level']
71+
72+
export default GreetingMessage
73+
```
74+
75+
For more information on the usage of the new functional components and the changes to render functions in general, see:
76+
77+
- [Migration: Render Functions](TODO)
78+
- [Guide: Render Functions](/guide/render-function.html)

0 commit comments

Comments
 (0)