CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

04 July, 2022

VueJS Render Function

 Programing Coderfunda     July 04, 2022     Vue.js Tutorial     No comments   

 We have seen components and the usage of it. For example, we have a content that needs to be reused across the project. We can convert the same as a component and use it.

Let’s take a look at an example of a simple component and see what the render function has to do within it.

Example

<html>
<head>
<title>VueJs Instance</title>
<script type = "text/javascript" src = "js/vue.js"></script>
</head>
<body>
<div id = "component_test">
<testcomponent></testcomponent>
</div>
<script type = "text/javascript">
Vue.component('testcomponent',{
template
: '<h1>Hello World</h1>',
data
: function() {
},
methods
:{
}
});
var vm = new Vue({
el
: '#component_test'
});
</script>
</body>
</html>

Consider the above example of a simple component that prints Hello World as shown in the following screenshot.

Render Function

Now, if we want to reuse the component, we can do so by just printing it again. For example,

<div id = "component_test">
<testcomponent></testcomponent>
<testcomponent></testcomponent>
<testcomponent></testcomponent>
<testcomponent></testcomponent>
</div>

And the output will be the following.

Component Reuse

However, now we need some changes to the component. We don’t want the same text to be printed. How can we change it? In case, we type something inside the component, will it be take into consideration?

Let us consider the following example and see what happens.

<div id = "component_test">
<testcomponent>Hello Jai</testcomponent>
<testcomponent>Hello Roy</testcomponent>
<testcomponent>Hello Ria</testcomponent>
<testcomponent>Hello Ben</testcomponent>
</div>

The output remains the same as we had seen earlier. It does not change the text as we want.

Component Reuse

Component does provide something called as slots. Let’s make use of it and see if we get the desired results.

Example

<html>
<head>
<title>VueJs Instance</title>
<script type = "text/javascript" src = "js/vue.js"></script>
</head>
<body>
<div id = "component_test">
<testcomponent>Hello Jai</testcomponent>
<testcomponent>Hello Roy</testcomponent>
<testcomponent>Hello Ria</testcomponent>
<testcomponent>Hello Ben</testcomponent>
</div>
<script type = "text/javascript">
Vue.component('testcomponent',{
template
: '<h1><slot></slot></h1>',
data
: function() {
},
methods
:{
}
});
var vm = new Vue({
el
: '#component_test'
});
</script>
</body>
</html>

As seen in the above code, in the template we have added slot, hence now it takes the value to send inside the component as shown in the following screenshot.

Slot Example

Now, let us consider we want to change the color and size. For example, currently we are using h1 tag and we want to change the HTML tag to p tag or div tag for the same component. How can we have the flexibility to carry out so many changes?

We can do so with the help of 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.

Example

<html>
<head>
<title>VueJs Instance</title>
<script type = "text/javascript" src = "js/vue.js"></script>
</head>
<body>
<div id = "component_test">
<testcomponent :elementtype = "'div,red,25,div1'">Hello Jai</testcomponent>
<testcomponent :elementtype = "'h3,green,25,h3tag'">Hello Roy</testcomponent>
<testcomponent :elementtype = "'p,blue,25,ptag'">Hello Ria</testcomponent>
<testcomponent :elementtype = "'div,green,25,divtag'">Hello Ben</testcomponent>
</div>
<script type = "text/javascript">
Vue.component('testcomponent',{
render
:function(createElement){
var a = this.elementtype.split(",");
return createElement(a[0],{
attrs
:{
id
:a[3],
style
:"color:"+a[1]+";font-size:"+a[2]+";"
}
},
this.$slots.default
)
},
props
:{
elementtype
:{
attributes
:String,
required
:true
}
}
});
var vm = new Vue({
el
: '#component_test'
});
</script>
</body>
</html>

In the above code, we have changed the component and added the render function with props property using the following piece of code.

Vue.component('testcomponent',{
render
:function(createElement){
var a = this.elementtype.split(",");
return createElement(a[0],{
attrs
:{
id
:a[3],
style
:"color:"+a[1]+";font-size:"+a[2]+";"
}
},
this.$slots.default
)
},
props
:{
elementtype
:{
attributes
:String,
required
:true
}
}
});

The props look like the following.

props:{
elementtype
:{
attributes
:String,
required
:true
}
}

We have defined a property called elementtype, which takes attributes field of type string. Another required field, which mentions that the field is mandatory.

In the render function, we have used the elementtype property as seen in the following piece of code.

render :function(createElement){
var a = this.elementtype.split(",");
return createElement(a[0],{
attrs
:{
id
:a[3],
style
:"color:"+a[1]+";font-size:"+a[2]+";"
}
},
this.$slots.default
)
}

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

CreateElement is taking the first param as the elementtag to be created. It is passed to the component using the following piece of code.

<testcomponent  :elementtype = "'div,red,25,div1'">Hello Jai</testcomponent>

The component needs to take the props field as shown above. It starts with : and the name of the props. Here, we are passing the element tag, color, fontsize, and the id of the element.

In render function, in createElement, we are splitting on comma, so the first element is the elementtag, which is given to the createElemet as shown in the following piece of code.

return createElement(
a
[0],{
attrs
:{
id
:a[3],
style
:"color:"+a[1]+";font-size:"+a[2]+";"
}
},
this.$slots.default
)

a[0] is the html element tag. The next parameter is the attributes for the element tag. They are defined in the attr field in the following piece of code.

attrs:{
id
:a[3],
style
:"color:"+a[1]+";font-size:"+a[2]+";"
}

We have defined two attributes for the element tag - id and style. To id, we are passing a[3], which is the value we have after splitting on comma. Using style, we have defined color and fontsize.

Last is the slot, that is the message we have given in the componentin the following piece of code.

<testcomponent :elementtype = "'div,red,25,div1'">Hello Jai</testcomponent>

We have defined the text to be printed in the createElement using the following piece of code.

this.$slots.default

It takes the default assigned in the component field.

Following is the output we get in the browser.

Component Field

The elements also show the structure. These are the components we have defined −

<div id = "component_test">
<testcomponent :elementtype = "'div,red,25,div1'">Hello Jai</testcomponent>
<testcomponent :elementtype = "'h3,green,25,h3tag'">Hello Roy</testcomponent>
<testcomponent :elementtype = "'p,blue,25,ptag'">Hello Ria</testcomponent>
<testcomponent :elementtype = "'div,green,25,divtag'">Hello Ben</testcomponent>
</div>
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • Vue.js Watch PropertyVue.js Watch PropertyThe Vue.js Watcher or watch property allows the developers to listen to the component data and run whenever they change a particu… Read More
  • Vue.js Event HandlingVue.js Events In Vue.js, Events are used to respond to an action. Suppose, you have to build a dynamic website using Vue.js then you'll most like… Read More
  • Vue.js Data BindingVue.js Data BindingVue.js supports different types of data binding. Before going to learn data binding in Vue.js, let's see what data binding is and h… Read More
  • Vue.js Transition and Animation Vue.js Transition and AnimationVue.js provides several ways to apply transition and animation effects to the applications when you insert, updat… Read More
  • Vue.js Form Input BindingsVue.js Form Input BindingsVue.js provides a v-model directive that can be used to create two-way data bindings on form inputs, text, textare… Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Sitaare Zameen Par Full Movie Review
     Here’s a  complete Vue.js tutorial for beginners to master level , structured in a progressive and simple way. It covers all essential topi...
  • Credit card validation in laravel
      Validation rules for credit card using laravel-validation-rules/credit-card package in laravel Install package laravel-validation-rules/cr...
  • C++ in Hindi Introduction
    C ++ का परिचय C ++ एक ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग लैंग्वेज है। C ++ को Bjarne Stroustrup द्वारा विकसित किया गया था। C ++ में आने से पह...
  • Write API Integrations in Laravel and PHP Projects with Saloon
    Write API Integrations in Laravel and PHP Projects with Saloon Saloon  is a Laravel/PHP package that allows you to write your API integratio...
  • iOS 17 Force Screen Rotation not working on iPAD only
    I have followed all the links on Google and StackOverFlow, unfortunately, I could not find any reliable solution Specifically for iPad devic...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (69)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (4)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

  • Sitaare Zameen Par Full Movie Review - 7/7/2025
  • Step-by-step Vue.js Tutorial Beginner to Master - 7/7/2025
  • Tailwindcss best practices for responsive design - 7/1/2025
  • Tailwind CSS Tutorial (Beginner to Master) - 7/1/2025
  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024

Laravel News

  • Add QR Code field functionality to your Filament UI's - 7/15/2025
  • Early View Data Preparation with Laravel View Creators - 7/14/2025
  • AI-Driven Development Insiders Launch: 500 Seats. 24 Hours. 50% Off - 7/14/2025
  • Convert Eloquent Models to HLS Video - 7/15/2025
  • Laravel's toUri() Method for Dynamic URL Construction - 7/14/2025

Copyright © 2025 CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda