How to use stateless components in Vue.js
Tags: vue, javascript, tutorial
Published: 2019-09-02 16:14:39

---
title: How to use stateless components in Vue.js
published: true 
tags: vue, javascript, tutorial 
canonical_url: https://blog.logrocket.com/how-to-use-stateless-components-in-vue-js/
---
**Written by [Nwose Lotanna](https://blog.logrocket.com/author/nwoselotanna/)**✏️

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/mt58yn017opqrnlyl73h.png)

In this post, you’ll learn about functional components and find out how to use stateless components in your workflow in Vue.

## Before you start

You will need the following in your PC:

- [Node.js version 10.x](https://nodejs.org/en/) and above installed. You can verify if you have this version of Node.js by running the command below in your terminal/command prompt:

```jsx
node -v
```

- [Visual Studio Code editor](https://code.visualstudio.com/) (or a similar code editor)
- [Vue’s latest version](https://vuejs.org/) installed globally on your machine
- [Vue CLI 3.0](https://cli.vuejs.org/) installed on your machine

To do this, uninstall the old CLI version first:

```jsx
npm uninstall -g vue-cli
```

Next, install the new one:

```jsx
npm install -g @vue/cli
```

- Download a Vue starter project [here](https://github.com/viclotana/vue-canvas)
- Unzip the downloaded project
- Navigate into the unzipped file and run the command to keep all the dependencies up-to-date:

```jsx
npm install
```

## Introduction: what are states and instances?

A Vue application state is an object that determines the behaviors of a component. Vue application states dictate how the component renders or how dynamic it is.

Meanwhile, a Vue instance is a ViewModel that contains options including templates for presentation elements, elements to be mounted, methods, and lifecycle hooks when initialized.

[![LogRocket Free Trial Banner](https://i0.wp.com/blog.logrocket.com/wp-content/uploads/2017/03/f760c-1gpjapknnuyhu8esa3z0jga.png?resize=1200%2C280&ssl=1)](https://logrocket.com/signup/)

## Vue components

Components in Vue.js are usually reactive: in Vue.js, data objects can have a lot of options for concepts you can use, computed properties, methods, and watchers. Additionally, data objects re-render any time data values change.

By contrast, functional components do not hold state.

## Functional components

Essentially, functional components are functions that have components of their own. Functional components do not have states or instances because they don’t keep or track state. Furthermore, you can’t access the construct within functional components.

Functional components were created for presentation. Functional components in Vue.js are similar to those in React.js. In Vue, developers can use functional components to build straight-forward, neat components easily by passing context.

## Functional components syntax

From [the official documentation](https://vuejs.org/v2/guide/render-function.html#Functional-Components), a functional component looks like this:

```jsx
Vue.component('my-component', {
  functional: true,
  // Props are optional
  props: {
    // ...
  },
  // To compensate for the lack of an instance,
  // we are now provided a 2nd context argument.
  render: function (createElement, context) {
    // ...
  }
})
```

## Creating a functional component

One key guideline to keep in mind when creating functional components is functional property. The functional property is specified either in the template section of your component or in the script section. The template section syntax looks something like this:

```jsx
<template functional>
  <div> <h1> hello world</h1>
  </div>
</template>
```

You can specify scripts as a property like this:

```jsx
export default {
  functional: true,
  render(createElement) {
    return createElement(
      "button", 'Click me'
    );
  }
};
```

## Why are functional components important?

Functional components can be executed quickly because they have no state and do not go through the same initialization and re-rendering process as components or parts of the template on data value change.

Mostly, functional components are useful for presentation or for displaying a loop of items.

## Demo

In this introductory demo, you’ll see both the single page component type demo with the Vue template and the render function type of functional components.

### Single-page functional component

Open your `Test.vue` file and copy the code block below into the file:

```jsx
<template functional>
  <div>
    <p v-for="brand in props.brands" :key="brand">{{brand}} </p>
  </div>
</template>
<script> 
export default {
  functional: true,
  name: 'Test',
  props: {
    brands: Array
  }
}
</script>
```

The functional indicator in both the script and the template shows that this is a functional component. Note that props can still be passed — they are the only data value that can be passed in functional components.

The temporal data props hold can also be looped through.

Open your `app.vue` file and copy the code block below into it:

```jsx
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <Test 
     :brands ="['Tesla', 'Bentley', 'Ferrari', 'Ford']">
    </Test>
  </div>
</template>
<script>
import Test from './components/Test.vue'
export default {
  name: 'app',
  components: {
    Test
  }
}
</script>
<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
```

Here, you’ll see that the props reference is being used with a colon.

Run the application in the dev server with the following command:

```jsx
npm run serve
```

The result in your browser should look like this:

![](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2019/09/stateless-components-corrected.png?resize=730%2C256&ssl=1)

### Render functions approach

Functional components can also contain render functions.

Developers use render functions to create their own virtual DOM without using the Vue template.

Use a render function to create a new button under the **cars** list. Create a new file inside your project folder named `example.js` and copy the code block below into the file:

```jsx
export default {
    functional: true,
    render(createElement, { children }) {
      return createElement("button", children);
    }
  };
```

This creates a render function in a functional component to show a button and uses the child node on the element as the button text.

Open your `app.vue` file and copy the code block below into the file:

```jsx
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <Test 
     :brands ="['Tesla', 'Bentley', 'Ferrari', 'Ford']">
    </Test>
    <Example>
     Find More Cars
    </Example>
  </div>
</template>
<script>
import Test from './components/Test.vue'
import Example from './Example'
export default {
  name: 'app',
  components: {
    Test, Example
  }
}
</script>
<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
```

If you run the application again, you’ll see that **Find More Cars** — the child node — is now the text for the button. The example component appears as a functional component upon inspection.

![](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2019/09/stateless-components-find-more-cars-corrected.png?resize=730%2C203&ssl=1)

### Adding click events

You can add click events on the components and include the method in your root component. However, you need the data object argument in your render function to access it.

Copy this in your `example.js` file:

```jsx
export default {
    functional: true,
    render(createElement, { data, children }) {
      return createElement("button", data, children);
    }
  };
```

Now, add your click event into the root component and Vue will recognize it. 

Copy the following into your `app.vue` file:

```jsx
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <Test 
     :brands ="['Tesla', 'Bentley', 'Ferrari', 'Ford']">
    </Test>
    <Example @click="callingFunction">
     Find More Cars
    </Example>
  </div>
</template>
<script>
import Test from './components/Test.vue'
import Example from './Example'
export default {
  name: 'app',
  components: {
    Test, Example
  },
  methods: {
    callingFunction() {
      console.log("clicked");
    }
  }
}
</script>
```

In addition to the above example, there are other arguments you can use in your functional components listed in the [official documentation](https://vuejs.org/v2/guide/render-function.html#Functional-Components).

## Conclusion

This beginner’s guide to functional components can help you achieve quick presentation, display a loop of items, or display simple parts of your workflow that do not require a state.
* * *
**Editor's note:** Seeing something wrong with this post? You can find the correct version [here](https://blog.logrocket.com/how-to-use-stateless-components-in-vue-js/).

## Plug: [LogRocket](https://logrocket.com/signup/), a DVR for web apps
 
![LogRocket Dashboard Free Trial Banner](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2017/03/1d0cd-1s_rmyo6nbrasp-xtvbaxfg.png?resize=1200%2C677&ssl=1)
 
[LogRocket](https://logrocket.com/signup/) is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.
 
In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.
 
[Try it for free](https://logrocket.com/signup/).
* * *
The post [How to use stateless components in Vue.js](https://blog.logrocket.com/how-to-use-stateless-components-in-vue-js/) appeared first on [LogRocket Blog](https://blog.logrocket.com).