The Complete Comparison between React vs Angular
Question Come in your mind. Should I choose Angular or React? Do you want to know about and discover the difference between React vs. Angular? Each framework has a lot to offer and it is not easy to choose both of them.
History of React vs. Angular
Angular has a large number of features out of the box than React. This can be both a good and a bad thing, depending on how you look at it.
Angular provides a lot of the features to the developer for a modern web application out of the box. Some of the standard features are:
- Dependency injection
- Templates, based on an extended version of HTML
- Routing, provided by @angular/router
- Ajax requests using @angular/common/HTTP
- @angular/forms for building forms
- Component CSS encapsulation
- XSS protection
- Utilities for unit-testing components.
Some of these features are built-in into the core of the framework and you don’t have an option not to use or change them. This requires developers to be familiar with features such as dependency injection to build even a small Angular web application. Other features such as the HTTP client or forms are completely optional and can be added on an as-needed basis.
React, gives you much more freedom. It only provides the “view” in MVC – you need to solve the M and C on your own. Due to this, you can choose any of your own libraries as you see fit. You will end up using many independent, fast-moving libraries. Because of this, you will need to take care of the corresponding updates and migrations by yourself. In addition, each React project is different and requires a decision requiring its folder hierarchy and architecture. Things can go wrong much more easily due to this.
- No dependency injection
- State management using set State and the Context API.
- XSS protection
- Utilities for unit-testing components.
Some of the popular libraries that are frequently utilized together with React are:
- React-router for routing
- Fetch (or axios) for HTTP requests
- A wide variety of techniques for CSS encapsulation
- Enzyme for additional unit-testing utilities.
Regular DOM vs. Virtual Dom
React’s use a virtual DOM and is one of its features that makes it so fast and quick. It was React’s “killer feature” when it was 1st released. Let me give you an example:
Let’s consider that you want to update a user’s age within a block of HTML tags. A virtual DOM only looks at the differences between the previous and current HTML and changes the part that is required to be updated. Get employs a similar method, which distinguishes the changes in a file.
Conversely, Angular opted to use a regular DOM. This will update the entire tree structure of HTML tags until it reaches the user’s age.
So why does this matter? The example above is trivial and probably won’t make any difference in a real app. However, if we’re dealing with hundreds of data requests on the same page (and the HTML block is replaced for every page request) it drastically affects the performance, in addition to the user’s experience.
There are several important things that come to mind when thinking about React: JSX, functional components, state management, Prop Types, and Flow.
In React you can define components using functions and classes. Functional components are usually pure and provide a clear mapping between in the input props and the rendered output. Functional code is normally less coupled and easier to reuse and test. However, functional components in React have some limitations. For example, they cannot have a state as opposed to the class components. This requires the developers to switch between the two paradigms to make the best of both worlds.
The situation will change when the hooks proposal is finalized and released. This will allow functional components to have a state and use other features of class components, such as lifecycle hooks. We will then be able to write purely functional React applications
The idea of state management is important for both frameworks and React has several approaches to offer. Each component can have its own state, so you can use that to create stateful components for holding the state of a part of the application. This is known as the lifting state up a pattern. This, however, gets impractical as you need to store global state required in different parts of the application as well as manually pass data around different levels of the component tree. To mitigate this, React 16.3 introduced the Context API that allows you to make data available an all component tree levels without passing it around explicitly. Contexts don’t store the state themselves, they only expose the data, but if you wrap it in a stateful component you can implement a convenient natively supported way to store the state.
There are also third-party libraries for state management in React. Redux is a state management library inspired by Flux, but with some simplifications. The key concept of Redux is that the whole state of the application is represented by a single object, which is mutated by functions called reducers. Reducers themselves are pure functions and are implemented separately from the components. This enables better separation of concerns and testability.
MobX is an alternative library for managing the state of an application. Instead of keeping the state in a single immutable store, as Redux does, it encourages you to store only the minimal required state and derive the rest from it. It provides a set of decorators to define observables and observers and introduce reactive logic to your state.
Prop Types is an optional feature of React that can bring additional safety measures if you’re not using Flow or TypeScript. It allows you to define a set of validators for the props of components that will check their values at runtime. Since React 15.5 prop types have been moved to a separate prop-types library and are now completely optional. Considering its benefits, we advise to use it to improve the reliability of your application.
Unlike Typescript, which has a similar purpose, it does not require you to migrate to a new language and annotate your code for type checking to work. In Flow, type annotations are optional and can be used to provide additional hints to the analyzer. This makes Flow a good option if you would like to use static code analysis, but would like to avoid having to rewrite your existing code.
All three features can greatly improve your developer experience: JSX, Flow, and Prop Types allow you to quickly spot places with potential errors, and carefully choosing your approach to state management will help achieve a clearer structure for your project.
Angular has a few interesting things up its sleeve as well, namely TypeScript, RxJS, and Angular Elements, as well as its own approach to state management.
RxJS is a reactive programming library that allows for more flexible handling of asynchronous operations and events. It’s a combination of the Observer and Iterator patterns blended together with functional programming. RxJS allows you to treat anything like a continuous stream of values and perform various operations on it such as mapping, filtering, splitting or merging.
The library adopted by Angular in their HTTP module as well for some inside use. When you perform an HTTP request, it returns an Observable instead of the usual Promise. This approach opens the door for interesting possibilities, such as the ability to cancel a request, retry it multiple times or work with continuous data steams, such as web sockets. But this is just the surface. To master RxJS, you’ll need to know your way around different types of Observables, Subjects, as well as around a hundred methods and operators.
Similar to React, Angular components can store data in their properties and pass them to their children. If you need to access values in sibling components you can move it to a stateful service that can later be injected into the components. Since reactive programming and RxJS is a first-class citizen in Angular, you can make use of observables to recalculate parts of the state based on some input. This, however, can get tricky in larger applications since changing some variable can trigger a multi-directional cascade of updates that is difficult to follow.
NgRx, the most well-known state management library for Angular can make things easier. It’s inspired by Redux but also makes use of RxJS to watch and recalculate information in the state. Using NgRx can help you enforce an understandable unidirectional info data flow as well as reduce coupling in your code.
NGXS is another state management library inspired by Redux. In contrast to NgRx, NGXS strives to reduce boilerplate code by using modern TypeScript features and improve the learning curve and overall development experience.
We’ve found TypeScript to be a great tool for improving the maintainability of our projects, especially those with large code base or complex domain/business logic. Code written in TypeScript is more descriptive and easier to follow. RxJS introduces new ways of managing data flow in your project but does require you to have a good grasp of the subject. Otherwise, it can bring unwanted complexity to your project. Angular elements have the potential for re-using Angular components and it’s interesting to see how this plays out in the future.
Reference: React vs. Angular: The Complete Comparison