Javatpoint Logo
Javatpoint Logo

Vue.js Render functions

Vue.js recommends us to use templates to build HTML. Here, we can use the render function as a closer-to-the-compiler alternative to templates.

Vue.js render functions are also used along with Vue.js components. Most of the time, the render function is created by the Vue.js compiler. When you specify a template on your component, this template's content is processed by the Vue.js compiler that will return a render function. The render function essentially returns a virtual DOM node, which will be rendered by Vue.js in your browser DOM.

What is the virtual Document Object Model or "DOM"?

The virtual DOM allows Vue.js to render your component in its memory before updating your browser. This makes everything faster because it has to do only a few interactions with the browser. When Vue.js updates the browser DOM, it compares the updated virtual DOM to the previous one and updates the real DOM with the modified parts. That's how it enhances performance.

Let's take an example of a simple component and see what the render function puts the effect when we use it within the example.

Index.html file:

Index.js file:

Let's use a simple CSS file to make the output more attractive.

Index.css file:

After the execution of the program, you will see the following output:

Output:

Vue.js Render functions

Here, you can see that the component shows the above result as Hello Students. Now, if you reuse the component repeatedly, you will see that the result would be printed again and again. For example,

Output:

Vue.js Render functions

Suppose, you don't want the same text to be printed again and again so, what will you do? Let's make some changes and type something inside the component as follows:

Index.html file:

Index.js file will be same as above.

After the execution of the program, you will see the following output:

Output:

Vue.js Render functions

You can see that the output remains the same as earlier. It does not change the text as we want.

Use of Slot

Now, we will use slots that are provided by components to get the desired result.

Syntax:

See the following example:

Index.html file:

Index.js file:

After the execution of the program, you will see the following output:

Output:

Vue.js Render functions

How to use render Function?

Suppose, you have to change the color and size of the results in output for example, we were using h1 tag in our previous examples and we want to change the HTML tag to p tag or div tag or any other tag for the same component. We have all the flexibility to do the changes by using the render function. Render function helps make the component dynamic and use the way it is required by keeping it common and helping pass arguments using the same component.

Let's see an example to demonstrate the use of render function:

Index.html file:

Index.js file:

After the execution of the program, you will see the following output:

Output:

Vue.js Render functions

Example Explanation

In the above example, you can see that we have changed the component and added the render function with props property using the following code in the Index.js file:

Here, the props property looks like the following code:

When we use components with render functions, they do not have a template tag or property. Instead of them, they define a function called render that receives a createElement in the following structure:

Syntax:

You can see it in the example as follows:

We have also defined a property named elementtype, which takes attributes field of type string. In another required field, we have mentioned that the field is mandatory.

See the render function code where we have used the elementtype property as follows:

The render function gets createElement as the argument and returns the same. The CreateElement creates the DOM element in the same way as it does in JavaScript. We have also split the elementtype on comma, using the values in the attrs field.

CreateElement takes the first param as the elementtag to be created. It is passed to the component as follows:

The component accepts the props field as the above code. It starts with the symbol : and after that the name of the props is specified. After specifying the prop's name, we have to pass the element tag, color, fontsize, and the id of the element.

In the render function, you can see that the first element is the elementtag in createElement field which is given to the createElemet as follows:

In the above code, the a[0] is the html element tag. The next parameters are the attributes for the element tag. They are defined in the attr field as follows:

Here, we have defined two attributes for the element tag: id tag and style tag.

  • In the id tag, we have passed a[3], which is the value we have after splitting on comma.
  • In the style tag, we have passed a[1] and a[2] respectively to define color and fontsize.

The message we have given in component is specified as follows:

The slots field is used to define the text that we want to print in the createElement using the following code:

After the execution of the program when you see the output, you will see that all the output entries are displayed in a specific way. This is because of the elements f the component structure we have defined in a specific way.

Event binding in Vue.js render functions

Let's see an example to demonstrate event binding in Vue.js render function. See the following example:

Example

Index.html file:

Index.js file:

After the execution of the program, you will see the following output:

Output:

Vue.js Render functions

When you click on the "Click me" button, you will see that the number of clicks has been displayed as how many times you have clicked on the button. See the output:

Vue.js Render functions

In the above example, the button is clicked four times.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA