Angular2 vs React

angularvsreact

A comparison between Angular2 vs React from the perspective of an enterprise C# developer

Currently we had a lot of discussion on which web framework we should invest: Angular2 or React. Therefore, I decided to experiment with both and make up my own opinion.

I use Angular1 since a couple of years now and I love it. Nevertheless, hearing about all the goodness of React, I wanted to give it a try and compare both frameworks with an open mind.

So, I reviewed these technologies according to my own perspective, the perspective of an enterprise .Net developer.  In my world applications have a lifecycle that can easily stretch over 10 years. Therefore, be able to support big projects and keeping maintaining them over time, is key. This is why I took Typescript as a given.  Coming from a .Net background and having built all my Angular apps with the help of Typescript I don’t wanted to get back to pure JavaScript. I love the static type checking and all the tooling support, code completion and refactoring typescript brings to the table.  The ease of programming inside the frameworks with Typescript is for me an important feature. This is why I tested both frameworks using Typescript.

React

React is a declarative Javascript UI library, created by Facebook that handles a very specific set of concerns, composing and packaging your UI in components and rendering these components. What could seem odd in the beginning is that React doesn’t separate the HTML from the JavaScript.  Frankly I got used to it and learned to appreciate the advantages of this. You can still separate the logic from the presentation by creating components that only render Html and components that handles the logic  (named “Controller Views” components).

Here is a simple example of a React component:

[snippet id=”371″]

The render() method is the core method of the React component.  React implements a virtual DOM, this is an internal representation of the DOM.

A react component is written in a JSX file.  JSX basically enables to mix HTML with JavaScript, you can also directly use js files in React but then you lose some syntactic sugar.   To transform the JSX at runtime or compile time you typically need to use Babel. Now TypeScript provide support for JSX, this enable to use the Typescript compiler instead of Babel.  To enable support for JSX with the Typescript compiler you need to add the compiler option: “jsx”:”react” in your tsconfig.json file.  When this is done, you can use the “TSX” extensions, this is the equivalent of “JSX” but for Typescript.

I found that VisualStudio 2015 and VS Code provided a good support for the TSX format.

fig1
With TSX you get auto completion in the HTML parts.

To illustrate other key concepts of React we need a more sophisticated example:

fig2

Notice on line 9, we pass two type arguments to declare our AuthorComponent class.  The first is the “prop” the other is the “state”.  These two concepts should not be confused.  “state” is an internal concern of the component, it holds the internal data (model) of the component and can change. Every time it changes the virtual DOM is adapted.  “props” are passed to the component and are not meant to be changed.  An illustration of the use of “props” is with “Controller Views Components”, these are parent components that host child components. “props” are typically used to pass the component properties:

fig3
author, onChange, onSave, errors are “props” of the AuthorForm component

Angular2 is a Framework,  React is only a UI library, therefore it’s usually used together with other libraries like Redux.  Redux help us to manage the application data flows.  You don’t absolutely need Redux but usually when building large scaled enterprise applications it’s something you’ll want to add to your React apps.  In fact you can even use Redux with Angular but the unidirectional flow of React lent itself very well to the Redux model.  Redux is based on the original Facebook Flux library. It helps at managing the application State. I found that managing state in a React application without Flux or Redux demand a considerable effort. Nevertheless, these frameworks also add a lot of supplementary concepts to learn.  Explaining these is outside the scope of this article.

Angular2

Although Angular2 comes with a lot of breaking changes and you can’t upgrade directly from Angular1, Angular2 still feels like an evolution of Angular1. Despite all the concept changes, switching from Angular1 to Angular2 was relatively easy. Nevertheless, I still have troubles with the new Angular2 binding syntax.

Angular2 advocates the separation of HTML and Code.  Although these can now be in the same file you never mix JavaScript & HTML in Angular2.   This is usually a good practice especially when working with web designers but as you use a specific data-binding syntax you don’t get the code highlighting & code completion you get with React.

Angular2 is quite complete and opinionated framework.  With Angular2 you get all in a box.  You can replace many parts but I never really needed to.  From an enterprise viewpoint, be able to count on a complete but modular toolbox is an advantage.  It helps new hired developers to be productive faster and enforce consistency.

Here is an example of what a “Hello world” Angular2 app looks like.  A tutorial is available on the angular quickstart guide, it demonstrates how to build a basic Angular app.  It took me less than 5 minutes to complete this tutorial. Once completed you’ve the basic building blocks: a project structure, a configured typescript compiler and a small web server that keeps the app running while you build your app.

Angular2 separate the HTML from the code, you can add the Html inside a Component but it’s still in a separate section. To be able to add view logic inside the HTML you need to use the Angular2 template syntax. A simple Angular HTML component looks like this:

fig4

The *ngFor, {{todo.title}}, (click), [todo] and <todo-detail> elements are all Angular2 template syntax.  The <todo-detail> tag represent a TodoListComponent.

The todolist.component.ts code could be like this:

fig5

Here is an example of how to define a two-way databinding works in Angular2:

<input type=”text” [(ngModel)]=”user.first_name” />

Every Angular2 app begins by declaring at least one module.  This is an example of such a module:

[snippet id=”381″]

My evaluation

Angular2 loves TypeScript

For me the biggest difference between both is that Angular2 is Typescript native and React just tolerates it. Sure, you can use Typescript with React, I provided some examples how you can do that. Nevertheless, I spend a lot of time figuring how.  This is especially a problem when you consider that the React examples you find on the web are inconsistent. During my experimentation with React I often was forced to use the <<any>> type.  This means that I lost the goodness of working with Typescript: compile time type checking, autocomplete and refactoring support.

Angular2 feels like home for C# devs

The React documentation provided by Facebook is quite good but the Angular2 official documentation is just superb.  Following the quickstart guide I was able to construct a full application in less than 5 minutes.  You can also find a complete guide that covers every topic of the framework.  The Angular2 “Tour of Heroes” is a tutorial where you build a real world application using every important aspect of Angular2.

Every example in the Angular2 documentation is provided in Typescript.  For React most documentation is in ES6 but on other internet sites most of the examples are still in ES5. Finding an up-to-date tutorial on how to bootstrap a standard React application with Typescript is not easy. You can find one on the TypeScript site but it’s very basic.  When you compare this to the Angular2 documentation where you even find guidance on how to setup VisualStudio, you immediately understand that Angular2 is far more welcoming for C# devs as React.  For React the only guidance I found on how to integrate Typescript was on the Typescript site itself. Usually you need to figure out by yourself how to use Typescript with React as you don’t find a lot on the internet. During my React experimental project I’ve wasted many hours converting Javascript examples to Typescript. I had some problems with inconsistencies between type definitions; sometimes I needed to tweak this myself to get my project compiled.  I didn’t have any of these problems using Angular2, the type definitions where always up to date and all provided libraries worked well together.

Mixing Html inside JavaScript is fine!

When working with React in VS.Code and VisualStudio, I was first disappointed as these editors didn’t support the JSX syntax correctly.  All this changed when I converted my first code to TSX (the TypeScript equivalent of JSX).  Here React really shined as I got type checking & code completion even inside my TSX templates.   Mixing typescript inside the HTML was for me a little revelation.  You don’t need to learn another programming or templating syntax. You can use the same programming language everywhere. When you use TypeScript you even get static type checking in your templates. Those who have already used Angular will certainly see the benefit of this.  With Angular I can’t count the number of times I got a blank page or things missing on my page because I misspelled some property in the template or used the wrong templating syntax.

ErrorMessages are better in React

Another great aspect of React is the explicit error messages it provides.
fig6

In Angular1, at best,  you usually get a blank screen with completely useless error messages. With the current version of Angular2 I found the error messages had improved. Most of the time the error messages returns explicit messages with descriptions on how you can solve the problem.
fig7

Many ways to do the same thing

A pain point concerning React is the many ways to do the same thing. For example I found the following ways to create a React component: using the ES5 Create Class Component, using the ES6 class component, using stateless components, with Object.Create, with Mixins, creating Parasitic Components and with the StampIt library.    Also, a primary concern for me, is the high churn rate of React, currently React is at version 15.  This is a high number for a framework that is only a couple of years old.

Conclusion

For me the conclusion is the following: if you are a C# enterprise developer who wants to get all the goodness Typescript brings to the table, Angular2 is the obvious choice.  React is a beautiful library that enforces good design but it doesn’t embrace Typescript as Angular2 does.  React has certainly some advantages compared to Angular2 like using JavaScript for the templating syntax. Nevertheless Angular2 is what an enterprise developer wants. It’s complete, opinionated and his Typescript native support makes it more crafted for large scale applications.  Angular2 two-way binding also result usually in a higher productivity rate and less code to maintain when it comes to building business applications.

 

 

 

To build this review I used following resources:

 

The source code of the example applications can be found on Github:

  • React-pluralsight: Simple demo app using TypeScript and React based on the pluralsight course, Building Applications with React and Flux

  • MerodeTeamManagement: Angular2 app using .Net Core as a backend

Leave a Reply