Vue.js functional components: what, why, and when?

Austin Gil
Jun 21, 2019 · 4 min read

tldr: If your component does not need to be stateful, turning it into a functional components can increase your its rendering performance by 70%.

What is a functional component?

Functional components (not to be confused with Vue’s render functions) is a component which holds no state and no instance.

In more plain English, this means that the component does not support reactivity and cannot make a reference to itself through the this keyword.

Image for post
Image for post
Functional Component with Vue Template
Image for post
Image for post
Functional Component with render function

Without the state or instance, you might wonder how you can reference things like data or methods. Fortunately, Vue provides a “context” parameter to the underlying render function.

This “context” argument is an object with the following properties:

  • props: An object of the provided props
  • children: An array of the VNode children
  • slots: A function returning a slots object
  • scopedSlots: (v2.6.0+) An object that exposes passed-in scoped slots. Also exposes normal slots as functions.
  • data: The entire data object, passed to the component as the 2nd argument of createElement
  • parent: A reference to the parent component
  • listeners: (v2.3.0+) An object containing parent-registered event listeners. This is an alias to data.on
  • injections: (v2.3.0+) if using the inject option, this will contain resolved injections.

Accessing this “context” parameter is pretty straightforward. For example, if we wanted to do something with props, it might look like this:

Image for post
Image for post
Accessing component context in template
Image for post
Image for post
Accessing component context in render function

In a functional component you actually don’t need to register the props that it accepts. However, if you do register them, they will still be validated against their configuration. I personally think it’s still a good idea to register them so I can specify the type, required, default value, and/or custom validators.

Why are functional components cool?

Functional components can make accessing your components a little more difficult to work with or introduce some complexity, so why go through the hassle?

Speed.

Because functional components do not have a state, they do not require extra initialization for things like Vue’s reactivity system.

Functional components will still react to changes like new props being passed in, but within the component itself, there is no way for it to know when it’s data has changed because it does not maintain its own state.

For those of you that prefer hard numbers, I did a benchmark for rendering 1,000 list items as stateful components vs. functional components, and the while the stateful components took on average 140ms, the functional components took about 40ms.

See for yourself:

https://codesandbox.io/s/vue-template-yterr?fontsize=14

For a large application, you will see significant improvements to the DOM render/update events after implementing functional components.

When would you use functional components?

Functional components are probably not right for many cases. After all, the point of using a JavaScript framework is to build applications which are more reactive. In Vue, you cannot do this without the reactivity system in place.

There are, however, some excellent use cases to sprinkle in functional components:

  • A component which is simply presentational, AKA a “dumb” component. For example, buttons, pills, tags, cards, even full pages with just static text like an About page.
  • A “higher order component” which is used to wrap markup or basic functionality around another component.
  • Any time you find yourself in a loop (v-for), the items in the loop are usually great candidates.

Caveats

There is one small issue I’ve run into for a pretty specific use case. When using a <template> tag, and accepting data through a prop, sometimes we want to modify the data inside the template.

With a standard Vue component, this is easy using either methods or computed props. With functional components, we do not have access to methods or computed props.

However, there is still a way to do this.

Let’s say our component accepts a "user" prop which is an object with "firstName" and "lastName", and we want to render a template that shows the user's full name.

In a functional <template> component, the way we could do this is by providing a method right on our component definition, then use the $options property provided by Vue to access our special method:

Image for post
Image for post
Computed properties in functional components

I have not personally run into this other issue, but a community member found that try to nest functional components that have scoped slots, the behavior is not the same as nesting stateful components with scoped slots https://github.com/vuejs/vue-loader/issues/1136.

Conclusion

If you care about performance or are working on very large applications, consider getting in the practice of creating functional components. The small learning curve is worth the performance gains.

References:
https://vuejs.org/v2/guide/render-function.html#Functional-Components
https://itnext.io/whats-the-deal-with-functional-components-in-vue-js-513a31eb72b0


Originally published at https://stegosource.com.

Vue.js Developers

Helping web professionals up their skill and knowledge of…

Austin Gil

Written by

Full Stack @Revealbio | Author of Vuetensils & Particles CSS | Panelist @ViewsOnVue | Board Member @sandiegojs

Vue.js Developers

Helping web professionals up their skill and knowledge of Vue.js

Austin Gil

Written by

Full Stack @Revealbio | Author of Vuetensils & Particles CSS | Panelist @ViewsOnVue | Board Member @sandiegojs

Vue.js Developers

Helping web professionals up their skill and knowledge of Vue.js

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store