Over the past decade, JavaScript has stood tall as the preferred language for front-end web development. In this dynamic landscape, React has emerged as the dominant framework, consistently holding the top position since 2016, as revealed by the State of JS survey. However, a subtle yet noteworthy trend has been unfolding—Vue is steadily gaining ground, offering a compelling alternative to React. In this article, we delve into the reasons behind this shift, comparing Vue and React across various aspects to empower developers in making informed choices.
Vue, crafted by Evan You in 2014, draws inspiration from AngularJS and React. What sets Vue apart is its independent evolution, free from the influence of large corporations like Google and Facebook. This autonomy has allowed Vue to incorporate diverse influences, providing a unique approach to front-end development.
The foundational structure of Vue and React projects shares similarities, utilizing HTML, JavaScript, and application entry files. Both rely on an HTML entry file (index.html), a JavaScript entry file (main.js), and an application entry file (App.vue in Vue or App.jsx in React). This structure ensures seamless integration of plugins and tools without disrupting application logic.
├── index.html
├── package.json
├── src
│ ├── App.vue
│ └── main.js
└── vite.config.js
While both frameworks adopt virtual DOM for efficient DOM manipulations, differences emerge in their syntax. Vue embraces directives influenced by AngularJS, featuring operations like for loops (v-for), conditionals (v-else-if), and event handling (v-on). React, in contrast, leverages JSX, allowing developers to employ familiar JavaScript syntax for tasks like for loops and event handlers.
import React, { useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
return (
<div>
<h2>Click the button, you know you want to</h2>
<button onClick={() => setCount(count + 1)}>
count is: {count}
</button>
</div>
);
}
Vue opts for a clearer separation of concerns within a single file component, breaking down into script, template, and style sections—a structure resembling the trio of languages used in web development: JavaScript, HTML, and CSS.
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
<template>
<button type=“button” @click=“count++”>
<style>To grasp the nuances of Vue in action, let’s embark on a tour of a simple Vue project and draw comparisons with a React counterpart.
In a Vue project, dependencies differ, with vue as a project dependency and two development dependencies: vite and @vitejs/plugin-vue. In contrast, a React project typically includes react and react-dom.
{
"name": "vite-vue-starter",
"dependencies": {
"vue": "^3.2.33"
},
"devDependencies": {
"@vitejs/plugin-vue": "^2.3.2",
"vite": "^2.9.8"
}
}
The file structure for both Vue and React projects remains similar, showcasing the adaptability of tools like Vite across frameworks.
vite.config.js
index.html
src
│ main.js (or main.jsx in React)
│ App.vue (or App.jsx in React)
components
│ HelloWorld.vue (or HelloWorld.jsx in React)
In the Vite configuration file, the framework specificity is explicitly declared, signaling to Vite that it is a Vue project.
// vite.config.js
import { defineConfig } from ‘vite’
import vue from ‘@vitejs/plugin-vue’
export default defineConfig({
plugins: [vue()]
})
The entry HTML file in Vue mirrors its React counterpart, loading the entire application within a div.
<!-- index.html -->
<html lang=“en”>
<head>
<!– Head content –>
</head>
<body>
<div id=“app”></div>
<script type=“module” src=“/src/main.js”></script>
</body>
</html>
In both Vue and React, the root component file, main.js (or main.jsx), imports the top-level application component (App.vue or App.jsx) and mounts it to the specified DOM element.
// src/main.js (or main.jsx in React)
import { createApp } from ‘vue’
import App from ‘./App.vue’
createApp(App).mount(‘#app’)
// Equivalent React main.jsx file
import React from ‘react’
import ReactDOM from ‘react-dom’
import App from ‘./App’
ReactDOM.render(<App />, document.getElementById(‘app’))
The main application component file (App.vue or App.jsx) encapsulates the logic, template, and styling, adhering to the respective framework’s conventions.
<!-- src/App.vue -->
<script setup>
import HelloWorld from ‘./components/HelloWorld.vue’
</script>
<template>
<HelloWorld msg=“Hello World” />
</template>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
margin-top: 3rem;
}
</style>
// Equivalent React App.jsx file
import HelloWorld from ‘./components/HelloWorld.jsx’
function App() {
return (
<div>
<HelloWorld />
</div>
)
}
export default App
The Hello World component file (HelloWorld.vue or HelloWorld.jsx) showcases the framework-specific syntax and structure.
<!-- src/components/HelloWorld.vue -->
<script setup>
import { ref } from ‘vue’
defineProps({
msg: String,
})
const count = ref(0)
</script>
<template>
<!– Template content –>
</template>
<style scoped>
/* Scoped styles */
</style>
// Equivalent React HelloWorld.jsx file
/* React component structure */
The collaboration of these components results in a complete Vue application, visually reflecting the essence of Vue’s clean separation of concerns and concise syntax.
Similar to React’s meta-frameworks like Next.js and Gatsby, Vue boasts its own meta-frameworks—Nuxt.js and Gridsome. Nuxt.js, drawing inspiration from Next.js, offers features such as SSR, API routes, serverless function integration, and third-party modules. These meta-frameworks bring additional capabilities, including static site generation, server-side rendering, and authentication, catering to diverse developer requirements.
In conclusion, the shift from React to Vue represents a nuanced exploration of front-end development. Despite variations in syntax and project structures, transitioning between the two frameworks remains feasible for developers. Vue’s concise syntax and clean separation of concerns offer an appealing alternative. Both ecosystems, while distinct, empower developers to build robust applications with a diverse set of tools and libraries. As we navigate the evolving landscape of front-end development, staying informed about the strengths and nuances of Vue and React ensures developers can seamlessly adapt to the demands of modern web development.
Subscribe to the Optimized Dev Newsletter to stay abreast of the latest tools, frameworks, and insights that enhance your web development journey.
© 2013 - 2024 Foreignerds. All Rights Reserved