Vue.js 3.0 Tutorial
Vue.js 3.0 Component Advanced
Vue.js 3.0 Transitions & Animations
Vue.js 3.0 Reusable & Combinable
Vue.js 3.0 Advanced
Vue.js 3.0 Tools
Vue.js 3.0 Scale
Vue.js 3.0 Accessibility
Vue.js 3.0 Migrating from Vue2
Vue.js 3.0 Contribute Documentation
Vue.js 3.0 API References
Vue.js 3.0 Style Guide
Functional components in Vue.js are stateless and instanceless components, making them much faster to render. They are ideal for components that don't manage any state and simply receive props. However, starting from Vue 3.0, the Vue team has removed the explicit functional
attribute for functional components due to its complexity.
Instead, if you want to create functional components, you can use the plain functions to create them. Functional components are just functions that take in props and context as arguments and return a Vue component.
Here is how you can create a functional component in Vue 3.0:
export default function MyFunctionalComponent(props, context) { return h('div', context.attrs, `Hello, ${props.name}`); }
In this example, h
is the createElement function, which is imported from vue
. The first argument is the HTML tag to be created, the second argument is an object of HTML attributes, and the rest of the arguments are children of this tag.
We can add this component to our parent component like this:
<template> <div> <MyFunctionalComponent :name="name" /> </div> </template> <script> import { h } from 'vue' import MyFunctionalComponent from './MyFunctionalComponent' export default { components: { MyFunctionalComponent }, data() { return { name: 'Vue 3.0' } } } </script>
In this example, MyFunctionalComponent
receives the name
prop and displays it.
Please note that functional components in Vue 3.0 lose many of the features that come with Vue components, such as data
, computed
, methods
, lifecycle hooks, and more. Therefore, you should only use functional components when you need a high-performance, stateless component.
Vue 3.0 props in functional components:
this
and must receive props as an argument.<!-- FunctionalComponent.vue --> <template functional> <div>{{ props.message }}</div> </template> <script> export default { props: ['message'], }; </script>
Vue 3.0 rendering with functional components:
<!-- RenderFunctionalComponent.vue --> <template functional> <div>{{ props.message }}</div> </template> <script> export default { props: ['message'], }; </script>
Vue 3.0 pure functional components vs regular components:
Description: Pure functional components are stateless and always return the same result for the same props, making them ideal for caching. Regular components may have internal state and lifecycle methods.
Code (Pure Functional):
<!-- PureFunctionalComponent.vue --> <template functional> <div>{{ props.message }}</div> </template>
Code (Regular Component):
<!-- RegularComponent.vue --> <template> <div>{{ message }}</div> </template> <script> export default { data() { return { message: 'Hello, Vue!', }; }, }; </script>
Vue 3.0 using render functions with functional components:
createElement
function to define the component's structure.<!-- RenderFunctionComponent.vue --> <script> export default { functional: true, render(createElement, context) { return createElement('div', context.props.message); }, props: { message: String, }, }; </script>
Vue 3.0 stateless functional components:
<!-- StatelessFunctionalComponent.vue --> <template functional> <div>{{ props.message }}</div> </template> <script> export default { props: ['message'], }; </script>
Vue 3.0 memoization in functional components:
memo
, computed
) to avoid unnecessary re-renders.<!-- MemoizedFunctionalComponent.vue --> <script> export default { functional: true, props: ['message'], render: function (h, context) { return h('div', this.$options.__m(this.message)); }, __m: memoize(message => message.toUpperCase()), }; function memoize(fn) { const cache = Object.create(null); return function (arg) { if (cache[arg]) { return cache[arg]; } return (cache[arg] = fn(arg)); }; } </script>