DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • React, Angular, and Vue.js: What’s the Technical Difference?
  • React Server Components (RSC): The Future of React
  • Cross-Platform Mobile Application Development: Evaluating Flutter, React Native, HTML5, Xamarin, and Other Frameworks
  • The Cypress Edge: Next-Level Testing Strategies for React Developers

Trending

  • Infrastructure as Code (IaC) Beyond the Basics
  • Understanding and Mitigating IP Spoofing Attacks
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • Using Python Libraries in Java
  1. DZone
  2. Coding
  3. JavaScript
  4. Switching From React to Vue.js

Switching From React to Vue.js

If you're caught trying to decide between these two great JavaScript frameworks, read on to get a Vue advocate's opinion on the matter.

By 
Anthony Gore user avatar
Anthony Gore
DZone Core CORE ·
Updated Feb. 04, 20 · Analysis
Likes (17)
Comment
Save
Tweet
Share
29.1K Views

Join the DZone community and get the full member experience.

Join For Free

So you're a React developer and you've decided to try out Vue.js. Welcome to the party!

React and Vue are kind of like Coke and Pepsi, so much of what you can do in React you can also do in Vue. There are some important conceptual differences though, some of which reflect Angular's influence on Vue.

I'll focus on the differences in this article so you're ready to jump into Vue and be productive straight away.

How Much Difference Is There Between React and Vue?

React and Vue have more similarities than differences:

  • Both are JavaScript libraries for creating UIs.
  • Both are fast and lightweight.
  • Both have a component-based architecture.
  • Both use a virtual DOM.
  • Both can be dropped into a single HTML file or be a module in a more sophisticated Webpack setup.
  • Both have separate, but commonly used router and state management libraries.

The big differences are that Vue typically uses an HTML template file whereas React is fully JavaScript. Vue also has mutable state and an automatic system for re-rendering called "reactivity."

We'll break it all down below.

Components

With Vue.js, components are declared with an API method .component which takes arguments for an id and a definition object. You'll probably notice familiar aspects of Vue's components and not-so-familiar aspects:

JavaScript
 




x
37


 
1
Vue.component('my-component', {
2

          
3
  // Props
4
  props: [ 'myprop' ],
5

          
6
  // Local state
7
  data() {
8
    return {
9
      firstName: 'John',
10
      lastName: 'Smith'
11
    }
12
  },
13

          
14
  // Computed property
15
  computed: {
16
    fullName() {
17
      return this.firstName + ' ' + this.lastName;
18
    }
19
  },
20

          
21
  // Template
22
  template: `
23
    <div>
24
      <p>Vue components typically have string templates.</p>
25
      <p>Here's some local state: {{ firstName }}</p>
26
      <p>Here's a computed value: {{ fullName }}</p>
27
      <p>Here's a prop passed down from the parent: {{ myprop }}</p>
28
    </div>
29
  `,
30

          
31
  // Lifecycle hook
32
  created() {
33
    setTimeout(() => {
34
      this.message = 'Goodbye World'  
35
    }, 2000);
36
  }
37
});



Template

You'll notice the component has a template property which is a string of HTML markup. The Vue library includes a compiler which turns a template string into a render function at runtime. These render functions are used by the virtual DOM.

You can choose not to use a template if you instead want to define your own render function. You can even use JSX. But switching to Vue just to do that would be kind of like visiting Italy and not eating pizza...

Lifecycle Hooks

Components in Vue have similar lifecycle methods to React components as well. For example, the created hook is triggered when the component state is ready, but before the component has been mounted on the page.

One big difference: there's no equivalent for shouldComponentUpdate. It's not needed because of Vue's reactivity system.

Re-Rendering

One of Vue's initialization steps is to walk through all of the data properties and convert them to getters and setters. If you look below, you can see how the message data property has a get and set function added to it:

switch_react_vue_1

Vue added these getters and setters to enable dependency tracking and change notification when the property is accessed or modified.

Mutable State

To change the state of a component in Vue you don't need a setState method, you just go ahead and mutate:

JavaScript
 




xxxxxxxxxx
1


 
1
// React
2
this.setState({ message: 'Hello World' });
3

          
4
// Vue
5
this.message = 'Hello World';



When the value of message is changed by the mutation, its setter is triggered. The set method will set the new value, but will also carry out a secondary task of informing Vue that a value has changed and any part of the page relying on it may need to be re-rendered.

If message is passed as a prop to any child components, Vue knows that they depend on this and they will be automatically re-rendered as well. That's why there's no need for a shouldComponentUpdate method on Vue components.

Main Template

Vue is more like Angular with regards to the main template file. As with React, Vue needs to be mounted somewhere in the page:

JavaScript
 




xxxxxxxxxx
1
10


 
1
<body>
2
  <div id="root"></div>
3
</body>
4
// React
5
ReactDOM.render('...', document.getElementById('root'));
6

          
7
// Vue
8
new Vue({
9
  el: '#root'
10
});



But unlike React, you can continue to add to this main index.html as it is the template for your root component.

HTML
 




xxxxxxxxxx
1


 
1
<div id="root">
2
  <div>You can add more markup to index.html</div>
3
  <my-component v-bind:myprop="myval"></my-component>
4
</div>



There's also a way to define your child component templates in the index.html as well by using HTML features like x-template or inline-template. This is not considered a best practice though as it separates the template from the rest of the component definition.

Directives

Again, like Angular, Vue allows you to enhance your templates with logic via "directives." These are special HTML attributes with the v- prefix, e.g. v-if for conditional rendering and v-bind to bind an expression to a regular HTML attribute.

JavaScript
 




xxxxxxxxxx
1
11


 
1
new Vue({
2
  el: '#app',
3
  data: {
4
    mybool: true,
5
    myval: 'Hello World'
6
  }
7
});
8

          
9
<div id="app">
10
  <div v-if="mybool">This renders if mybool is truthy.</div>
11
  <my-component v-bind:myprop="myval"></my-component>
12
</div>



The value assigned to a directive is a JavaScript expression, so you can refer to data properties, including ternary operators, etc.

Workflow

Vue doesn't have an official create-react-app equivalent, though there is the community built create-vue-app.

The official recommendation for bootstrapping a project, however, is vue-cli. It can generate anything from a simple project with one HTML file to a fully decked-out Webpack + Server-Side Rendering project:

Shell
 




xxxxxxxxxx
1


 
1
$ vue init template-name project-name 



Single HTML File Projects

Vue's creator Evan You dubbed his project a "progressive framework" because it can be scaled up for complex apps, or scaled down for simple apps.

React can do this too, of course. The difference is that Vue projects typically use less ES6 features and rarely use JSX, so there's usually no need to add Babel. Plus, the Vue library all comes in one file, there's no separate file for an equivalent of ReactDOM.

Here's how you add Vue to a single HTML file project:

HTML
 




xxxxxxxxxx
1


 
1
<script src="https://unpkg.com/vue/dist/vue.js"></script>


Note: if you don't intend to use template strings and therefore don't need the template compiler, there is a smaller build of Vue that omits this called vue.runtime.js. It's about 20KB smaller.

Single File Components

If you're happy to add a build step to your project with a tool like Webpack, you can utilize Vue's Single File Components (SFCs). These are files which have the .vue extension and encapsulate the component template, JavaScript configuration, and style all in a single file:

HTML
 




xxxxxxxxxx
1
13


 
1
<template>
2
  <div class="my-class">{{ message }}</div>
3
</template>
4
<script>
5
  export default {
6
    data() {
7
      message: 'Hello World'
8
    }
9
  }
10
</script>
11
<style>
12
  .my-class { font-weight: bold; }
13
</style>



These are without a doubt one of the coolest features of Vue because you get a "proper" template with HTML markup, but the JavaScript is right there so there's no awkward separation of the template and logic.

There's a Webpack loader called vue-loader which takes care of processing SFCs. In the build process, the template is converted to a render function so this is a perfect use case for the cut-down vue.runtime.js build in the browser.

Redux and More

Vue also has a Flux-based state management library called Vuex. Again, it's similar to Redux but has a number of differences.

I don't have time to cover it in this article so I'll cover it next week's article. Join my newsletter to get an email update when it's ready!


Further Reading

  • Create and Publish Web Components With Vue CLI 3.
  • Creating a Real-Time Data Application Using Vue.js.
React (JavaScript library) Vue.js Template HTML JavaScript

Published at DZone with permission of Anthony Gore, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • React, Angular, and Vue.js: What’s the Technical Difference?
  • React Server Components (RSC): The Future of React
  • Cross-Platform Mobile Application Development: Evaluating Flutter, React Native, HTML5, Xamarin, and Other Frameworks
  • The Cypress Edge: Next-Level Testing Strategies for React Developers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!