In this blog post, you’ll learn about making the right choices through comparing two popular front-end frameworks, Vue and React, by a set of objective criteria.
You will find it useful if you are to make a technology decision and want to understand what main differences are between React and Vue-based solutions.
Understanding the differences between React and Vue - Part 1
This article is part 1 in a two-part series about core differences between front-end frameworks: React and Vue. Click the links below to go back or skip ahead to the next article.
According to data from State of JS 2019 research, React is more popular than Vue, but the differences are not so significant.
Every one of us would like to use modern and fast applications with neat UI/UX. Fortunately, more and more business owners want to offer users the highest quality and the best experience from the services they develop. That means a lot of requirements for developers to provide fast and stable working of developed apps from both sides: backend and frontend. We'll focus on frontend frameworks that developers can use to create easy-to-use and quality code for smoothly working applications.
We'll take a look a bit deeper in Vue and React and discover what are the additional benefits of using frontend frameworks:
• The simplicity of the code
• Application environment
• State management
• Pace of development
First of all, a simple code is easy to maintain and able to be kept updated. The frameworks have some rules that need to be strictly followed.
Depends on the framework, the level of library integrations with the code is different. Some of the frameworks are remarkably flexible. Others enforce developers to use CLI tools, keep the fixed folder structure, and only minor modifications are permitted.
Based on given rules (because all frameworks always have any), the clear code is created. There is one priceless advantage: when a new developer would like to take a look into the system, earlier outcomes are easy to read and understand.
Using a particular framework means a fully prepared environment. There are some choices, but generally, the work is starting with some cardinal rules, tools, and features.
A developer may use view templates or state management, go over the best practices in documentation. Following those rules make the preparation of the environment for the app more accessible. Connections between each element are clear.
How to better manage the state? The frameworks could help us to handle state management properly, e.g., by third-party libraries designed for a particular framework.
Each new software developer in the project needs less time to read the source and documentation. Most programmers know well the framework base. They identify a lot of examples, tips, and best practices, so starting work on the solution from day one is not a problem. There is no need to think about every aspect of the app. Some reasonable solutions are provided by design in a framework (for example, routing or state management), and it is quicker to follow them than to prepare our own.
• Component lifecycle
• Virtual DOM
Templates are extremely helpful during development and can be reused in more than one project. Moreover, using DRY ("don’t repeat yourself") principle helps to produce more quality code.
JSX is something more than only vanilla HTML. This syntax produces the React components with given attributes (for example, inline styles, style class names, or props). It is possible to use loops for creating elements or render an array of them. The JSX based component could be returned from the method and stored in a variable, so it could be an argument for another function or wrapper.
Instead of templates in Vue.js, we can use rendering functions - the same situation as in the React case.
Reactivity means how the frameworks handle updated elements rerendering. Vue and React update only what is necessary.
React has a specific diffing algorithm. Reconciliation compares the new with the latest one and finds the best way to update the current view. React implements operation with the complexity of O(n) instead of O(n^3), and there are two assumptions:
Different elements = different trees. If the element type is changed (for example, from “div” to “span”), and the framework destroys all the sub-trees and then prepares it from scratch.
Using “key” props, we can choose which components should be stable across renders. It is why adding this prop to children elements is essential. React follows changes.
Every element comes with a corresponding watcher instance. The object registers changes during render as dependencies. In this case, when dependency “setter” is called, a watcher has notification about it. The element is updated based on this action.
All React components have the same lifecycle, from beginning to end. There are some options for change but three main steps are fixed (mounting, updating, unmounting). Each of them includes specific methods available for overriding.
• Mounting - in this step an instance of React Component is being created and inserted into the DOM. Examples lifecycle methods: construtor(), render(), componentDidMount();
• Updating - the element detects new changes in props or state for re-rendering. Examples lifecycle methods: render(), componentDidUpdate(); The element is updated if an algorithm detects any changes for it. It’s possible to control this feature manually or force to re-render using shouldComponentUpdate();
• Unmounting - an element is being removed from the DOM. Lifecycle method: componentWillUnmount().
More information about React Components Lifecycle, are available here.
The situation is very similar in the Vue.js case. During the component lifecycle, there are some steps named "hooks" (which can be called on each level): initialization, mounting, updating, destroying.
• Initialization - the element is created in two steps. First: events and life cycles, then injections and reactivity. Hooks: beforeCreate() and created().
• Mounting - in this step, we have access to the component before render. Hooks: beforeMount() and mounted().
• Updating - only this state can exist more than once, every time when the element is updating. Hooks: beforeUpdate(), updated().
• Destroying - the last component state. Here it is removed from DOM. Hooks: beforeDestroy(), destroyed().
Except for the descriptive cases, in Vue exists two more hooks that are “keep-alive” components (not rendered in the DOM). These methods are activated() and deactivated().
For more information about Vue.js lifecycle see here.
This concept is available for both technologies. The Virtual DOM is a type of container where the framework keeps all details about UI elements.
When the application is in use, a tree of custom objects represents a part of the DOM. All manipulations are carried out on virtual elements. The real DOM and the virtual one merge during rendering elements. The algorithm uses the virtual DOM to specify what is required to update in the browser.
As you can see, there are a lot of minor and major differences between React and Vue frameworks at first glance.
How to compare both technologies?
In the next article, React vs Vue in 2020. Differentiators Worth To Compare, we focus on comparison criteria, e.g., state management, flexibility, backward compatibility, or ease of learning. Stay tuned!
Do you have any comments or need to discuss a topic related to frontend technologies? We're more than happy to discuss it!
Author: Marta Zażlak