In my last article, we made a comparison of Angular, React and Vue by building a small web application using each. That concluded with React and Vue being the champion.
In this article, I would like to share my views on why I have become a big fan of Vue. I will try to be unbiased as much as possible. So, this in my own opinionated take on the question. Here’s why.
One of the most common element in a web app is to render a list element given an array. Let’s see the syntax in Vue and React.
React
<ul>
{
dataArray.map(data,i)=> <li key={i}>{data}</li>)
}
</ul>
react-list.js
Vue
<ul>
<li v-for="(data,i) in data" :key="i">
{{data}}
</li>
</ul>
vue-list.js
Now which looks more simpler, React’s jsx syntax or Vue’s html, you decide.
I prefer the cleaner vue’s syntax.
Below is the basic component syntax written in both.
class myComponent extends Component{
constructor(){
this.state = {
data:['Mango','Grapes','Guava']
}
}
render(){
const { data } = this.state;
return <ul>
{
data.map((data,i) =>
<li key={i}>
{data}
</li>
)
}
</ul>
}
}
react-component.js
<template>
<ul>
<li v-for="(data,i) in data" :key="i">
{{data}}
</li>
</ul>
</template>
<script>
export default {
data:()=>({
data:["Mango","Grapes","Guava"]
})
}
</script>
vue-component.js
Writing components should be simple with clear distinction in javascript part and html. Things look mixed up in react’s syntax (again completely my opinion).
Distinguishing between each of them becomes quite confusing for a newbie and i don’t blame them. Sheer number of lifecycles is a bit too much. Now in which lifecycle should we do an api call for fetching data.
Simple enough and all of them make sense. Most often we have to deal with created or mounted lifecycle.
Event handling should be very simple and straight forward since it is one of the most used feature of any javascript framework. Lets see how React and Vue handle this.
class myComponent extends Component{
constructor(){
this.state = {
data:['Mango','Grapes','Guava']
}
this.handleClick = this.handleClick.bind(this);
}
handleClick(){
console.log('list clicked')
}
render(){
const { data } = this.state;
return <ul onClick={this.handleClick}>
{
data.map((data,i) =>
<li key={i}>
{data}
</li>
)
}
</ul>
}
}
react-event.js
<template>
<ul @click="handleClick">
<li v-for="(data,i) in data" :key="i">
{{data}}
</li>
</ul>
</template>
<script>
export default {
data:()=>({
data:["Mango","Grapes","Guava"]
}),
methods:{
handleClick(){
console.log('list clicked')
}
}
}
</script>
vue-event.js
Vue provides very simple syntax for handling events where as in react, using this keyword then binding this in constructor seems unnecessary for a simple click handler.
Before wrapping, i would like to highlight one of the awesome feature that vue provides, computed property.
Lets say we have a prop called, dollars and we have to render converted rupees in ui. This becomes very simple with computed property. Below is how we’ll use it
<template>
<span>{{dollar}}$ in Rupees is ₹{{rupees}}</span>
</template>
<script>
export default {
props:{
dollar:Number
},
computed:{
rupees(){
return this.dollar * 68;
}
}
}
</script>
vue-compute.js
In my opinion, Vue is simpler than other current frameworks and thus makes the learning process easier not overwhelming. Hope you liked the article. If you did, please share a clap. Please feel free to suggest your own insights! Thanks for reading…
Tip: Easily share UI components across apps
Use Bit (open source) to easily share and manage reusable components between apps and projects- to build fatser! Give it a try:
☞ Vue.js 2 Academy: Learn Vue Step by Step
☞ Vue.js 2 Essentials: Build Your First Vue App
☞ Vue JS 2: From Beginner to Professional (includes Vuex)
☞ React vs Vue - I Built the Same App in Both Frameworks
☞ Vue js Tutorial Zero to Hero || Brief Overview about Vue.js || Learn VueJS 2023 || JS Framework
☞ JavaScript for React Developers | Mosh
☞ Vue.js Tutorial: Zero to Sixty