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

Vue 3.0 functional components

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.

  1. Vue 3.0 props in functional components:

    • Description: Functional components in Vue 3.0 can receive props just like regular components. However, they don't have access to this and must receive props as an argument.
    • Code:
      <!-- FunctionalComponent.vue -->
      <template functional>
        <div>{{ props.message }}</div>
      </template>
      
      <script>
      export default {
        props: ['message'],
      };
      </script>
      
  2. Vue 3.0 rendering with functional components:

    • Description: Functional components are useful for rendering presentational UI, as they have a simplified API. They receive props directly as an argument and return a render function.
    • Code:
      <!-- RenderFunctionalComponent.vue -->
      <template functional>
        <div>{{ props.message }}</div>
      </template>
      
      <script>
      export default {
        props: ['message'],
      };
      </script>
      
  3. 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>
      
  4. Vue 3.0 using render functions with functional components:

    • Description: Render functions provide more control over the rendering process. In functional components, you can use the createElement function to define the component's structure.
    • Code:
      <!-- RenderFunctionComponent.vue -->
      <script>
      export default {
        functional: true,
        render(createElement, context) {
          return createElement('div', context.props.message);
        },
        props: {
          message: String,
        },
      };
      </script>
      
  5. Vue 3.0 stateless functional components:

    • Description: Functional components are inherently stateless, making them suitable for pure presentation without managing any state.
    • Code:
      <!-- StatelessFunctionalComponent.vue -->
      <template functional>
        <div>{{ props.message }}</div>
      </template>
      
      <script>
      export default {
        props: ['message'],
      };
      </script>
      
  6. Vue 3.0 memoization in functional components:

    • Description: Functional components can benefit from memoization techniques (e.g., memo, computed) to avoid unnecessary re-renders.
    • Code:
      <!-- 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>