You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+134Lines changed: 134 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,6 +74,12 @@ List of 300 VueJS Interview Questions
74
74
|65 |[What are the directive Hook Arguments?](#what-are-the-directive-hook-arguments)|
75
75
|66 |[How do you pass multiple values to a directive?](#how-do-you-pass-multiple-values-to-a-directive)|
76
76
|67 |[What is function shorthand in directive hooks?](#what-is-function-shorthand-in-directive-hooks)|
77
+
|68 |[What is the benefit of render functions over templates?](#what-is-the-benefit-of-render-functions-over-templates)|
78
+
|69 |[What is a render function?](#What-is-a-render-function)|
79
+
|70 |[Explain the structure of createElement with arguments](#explain-the-structure-of-createelement-with-arguments)|
80
+
|71 |[How can you write duplicate virtual nodes in a component?](#how-can-you-write-duplicate-virtual-nodes-in-a-component)|
81
+
|72 |[List down the template equivalents in render functions?](#list-down-the-template-equivalents-in-render-functions)|
82
+
|73 |[What are functional components?](#what-are-functional-components)|
77
83
78
84
1.### What is VueJS?
79
85
**Vue.js** is an open-source, progressive Javascript framework for building user interfaces that aim to be incrementally adoptable. The core library of VueJS is focused on the `view layer` only, and is easy to pick up and integrate with other libraries or existing projects.
@@ -1440,3 +1446,131 @@ List of 300 VueJS Interview Questions
1440
1446
el.style.backgroundColor=binding.value
1441
1447
})
1442
1448
```
1449
+
68. ### What is the benefit of render functions over templates?
1450
+
In VueJS, the templates are very powerful and recommended to build HTML part of your application. However, some of the special cases like dynamic component creation based on input or slot value can be achieved through render functions. Also, these functions gives the full programmatic power of javascript eco system.
1451
+
69. ### What is a render function?
1452
+
Render function is a normal function which receives a `createElement` method as it’s first argument used to create virtual nodes. Internally Vue.js' templates actually compile down to render functions at build time. Hence templates are just syntactic sugar of render functions. Let's take an example of simple Div markup and corresponding render function,
1453
+
The HTML markup can be written in template tag as below,
1454
+
```javascript
1455
+
<template>
1456
+
<div :class="{'is-rounded': isRounded}">
1457
+
<p>Welcome to Vue render functions</p>
1458
+
</div>
1459
+
</template>
1460
+
```
1461
+
and the compiled down or explicit render function would appear as below,
1462
+
```javascript
1463
+
render:function (createElement) {
1464
+
returncreateElement('div',
1465
+
{ 'class': {
1466
+
'is-rounded':this.isRounded
1467
+
}
1468
+
},[ createElement('p', 'Welcome to Vue render functions')])
1469
+
},
1470
+
```
1471
+
**Note:** The react components are built with render functions inJSX.
1472
+
70. ### Explain the structure of createElement witharguments?
1473
+
The createElement accepts few arguments to use all the template features. Let us see the basic structure of createElement with possible arguments,
1474
+
```javascript
1475
+
// @returns {VNode}
1476
+
createElement(
1477
+
// An HTML tag name, component options, or async function resolving to one of these. Required.
1478
+
// Type is {String | Object | Function}
1479
+
'div',
1480
+
1481
+
// A data object corresponding to the attributes you would use in a template. Optional.
1482
+
//Type is {Object}
1483
+
{
1484
+
// Normal HTML attributes
1485
+
attrs: {
1486
+
id: 'someId'
1487
+
},
1488
+
// Component props
1489
+
props: {
1490
+
myProp: 'somePropValue'
1491
+
},
1492
+
// DOM properties
1493
+
domProps: {
1494
+
innerHTML: 'This is some text'
1495
+
},
1496
+
// Event handlers are nested under `on`
1497
+
on: {
1498
+
click: this.clickHandler
1499
+
},
1500
+
// Similar to `v-bind:style`, accepting either a string, object, or array of objects.
1501
+
style: {
1502
+
color: 'red',
1503
+
fontSize: '14px'
1504
+
},
1505
+
//Similar to `v-bind:class`, accepting either a string, object, or array of strings and objects.
1506
+
class: {
1507
+
classsName1: true,
1508
+
classsName2: false
1509
+
},
1510
+
....
1511
+
},
1512
+
1513
+
// Children VNodes, built using `createElement()`, or using strings to get 'text VNodes'. Optional.
1514
+
// Type is {String | Array}
1515
+
[
1516
+
'Learn about createElement arguments.',
1517
+
createElement('h1', 'Headline as a child virtual node'),
1518
+
createElement(MyComponent, {
1519
+
props: {
1520
+
someProp: 'This is a prop value'
1521
+
}
1522
+
})
1523
+
]
1524
+
)
1525
+
```
1526
+
71. ### How can you write duplicate virtual nodes in a component?
1527
+
All virtual nodes(VNodes) in the component tree must be unique.i.e, You can't write duplicated nodes in a straightforward way. If you want to duplicate the same element/component many times then you should use factory function.
1528
+
The below render function is invalid where you are trying to duplicate h1 element 3 times,
1529
+
```javascript
1530
+
render: function (createElement) {
1531
+
var myHeadingVNode = createElement('h1', 'This is a Virtual Node')
return createElement('h1', 'This is a Virtual Node')
1543
+
})
1544
+
)
1545
+
}
1546
+
```
1547
+
72. ### List down the template equivalents in render functions?
1548
+
VueJS provides proprietary alternatives and plain javascript usage for the template features. Let's list down them in a table for comparision,
1549
+
| Templates | Render function |
1550
+
|---- | --------- | ---- |
1551
+
| Conditional and looping directives: v-if and v-for | Use JavaScript’s if/else and map concepts|
1552
+
| Two-way binding: v-model | Apply own JS logic with value binding and event binding |
1553
+
| Capture Event modifiers: .passive, .capture, .once and .capture.once or .once.capture| &, !, ~ and ~! |
1554
+
| Event and key modifiers: .stop, .prevent, .self, keys(.enter, .13) and Modifiers Keys(.ctrl, .alt, .shift, .meta) | Use javascript solutions: event.stopPropagation(), event.preventDefault(), if (event.target !== event.currentTarget) return, if (event.keyCode !== 13) return and if (!event.ctrlKey) return |
1555
+
| Slots: slot attributes | Render functions provide this.$slots and this.$scopedSlots instance properties|
1556
+
73. ### What are functional components?
1557
+
The functional components are just simple functions to create simple components just by passing a context. Every functional component follows two rules,
1558
+
1. **Stateless:** It doesn’t keep any state by itself
1559
+
2. **Instanceless:** Iit has no instance, thus no this
1560
+
1561
+
You need to define `functional: true` to make it functional. Let's take an example of functional components,
1562
+
```javascript
1563
+
Vue.component('my-component', {
1564
+
functional:true,
1565
+
// Props are optional
1566
+
props: {
1567
+
// ...
1568
+
},
1569
+
// To compensate for the lack of an instance,
1570
+
// we are now provided a 2nd context argument.
1571
+
render:function (createElement, context) {
1572
+
// ...
1573
+
}
1574
+
})
1575
+
```
1576
+
**Note:** The functional components are quite popular in React community too.
0 commit comments