1. Code
  2. JavaScript
  3. React

Angular vs. React: 7 Key Features Compared

Scroll to top

Angular vs. React is a popular debate among front-end JavaScript developers and, more often than not, the discussion ends up being biased towards one technology or the other. Developed by Google and Facebook respectively, Angular and React are the two popular technologies used to build interactive single-page applications.

A comprehensive comparison between Angular and React is imminent because there are certain places in which they significantly overlap in terms of what they offer, i.e. building the front-end view of your application and other places where their functionality remains incomplete unless helped by a third-party library. Adopting one technology over the other is a question of whether Angular or React better solves your problem and a bit of intuition. In this tutorial, we’ll compare and contrast seven key different features of Angular and React.

I am an ardent proponent of the code-first approach (code speaks louder than words, they say). Keeping this in mind, I’ve added code samples of Angular and React wherever possible so that you can build on your intuition and decide which works for you and which doesn’t. Let’s get started.

Framework vs. Library

Angular is a framework, while React is a library.

So what does this mean? React on its own isn't adequate to create a web application because it is just designed to create views: the ‘V’ in MVC. React lets you build component-based views for which data can be passed down to child views. To address some of the other architectural needs, the React community has created key libraries like Redux and React Router, which supply architectural patterns that complement React.

Here's an illustration of the basic flow of the Redux architecture:

An image that depicts how the Flux architecture complements ReactAn image that depicts how the Flux architecture complements ReactAn image that depicts how the Flux architecture complements React

User interface events in the React components create actions, which update the central data store (model) of the app, which causes the components to re-render.

Most React apps will make use of these third-party libraries, and many more besides. Part of the challenge of starting out as a React developer is getting a handle on which third-party libraries are essential and learning these on top of React itself.

Angular, on the other hand, is more of a complete solution. 

Angular is a framework for building client applications.

Angular was firmly built on top of the MVC pattern, which separates the application into three different layers. The first version of Angular made this architecture very clear, but the added complexity involved in mastering concepts such as directives, factories, and services to create a single-page application forced the developers at Google to shift towards a component-based architecture.

But when your application starts to grow, it’s important to have a solid structure that keeps the business logic of your application away from the components. Being a framework, Angular allows you to enforce structural organization by moving the business rules into a domain model (using a combination of model classes and services) and injecting the model into your components via dependency injection.

Here is a sample of code that illustrates how the business logic is encapsulated inside a User model and a User service, and away from our component.

1
/* Path: /app/models/User.ts */
2
3
export class User {
4
    id: number;
5
    username: string;
6
    password: string;
7
    firstName: string;
8
    lastName: string;
9
}
1
/* /app/services/user.service.ts */
2
3
import { Injectable } from '@angular/core';
4
import { Http } from '@angular/http';
5
 
6
import { User } from '../models/User';
7
 
8
@Injectable()
9
export class UserService {
10
    constructor(private http : Http) { }
11
 
12
    getAll() {
13
        // API to return all users

14
    }
15
 
16
    create(user: User) {
17
        //API call to create user

18
    }
19
 
20
    update(user: User) {
21
        //API call to update user

22
    }
23
 
24
    delete(id: number) {
25
        //API call to delete user

26
    }
27
 
28
}
1
/* Path: /app/page/page.component.ts */
2
3
import { Component } from '@angular/core';
4
 
5
import { User } from '../models/User';
6
import { UserService } from '../services/user.service';
7
 
8
@Component({
9
    templateUrl: 'page.component.html'
10
})
11
 
12
export class PageComponent {
13
    currentUser: User;
14
    users: User[] = [];
15
 
16
    constructor(private userService: UserService) {
17
       //Dependency is Injected inside the constructor's arguments

18
 
19
  
20
    deleteUser(id: number) {
21
        this.userService.delete(id).subscribe(() => { #Do Something});
22
    }
23
 
24
    private loadAllUsers() {
25
        this.userService.getAll().subscribe(users => { #Do something else });
26
    }
27
}
1
<!---Path: /app/home/page.component.html -->
2
3
<div class="title">
4
   
5
    <h2>All users:</h2>
6
    <ul>
7
        <li *ngFor="let user of users">
8
            {{user.username}} ({{user.firstName}} {{user.lastName}})
9
            - <a (click)="deleteUser(user.id)">Delete</a>
10
        </li>
11
    </ul>
12
   
13
</div>

Component-Based Approach

Both Angular and React are built around the idea of a component.

Components in Angular

Components are the most basic building block of a UI in an Angular application. An Angular application is a tree of Angular components.

What are components? In Angular, components are TypeScript classes that have a @Component decorator marked over them. Moreover, inside these decorators, we can define what Angular calls the meta-data, which includes the template, styles, selectors and so forth.

Component hierarchy in Angular is designed in such a way that you can associate structure and functionality under a single entity. Here is a high-level architectural overview of components and how this links to everything else in Angular.

The architecture of AngularThe architecture of AngularThe architecture of Angular

Data sharing among components is possible by nesting components, as exemplified below.

1
/* UserParentComponent.ts */
2
3
import { Component } from '@angular/core';
4
 
5
 
6
// The <user-child> selector is nested inside <user-parent>. Each user is passed down as a property. 

7
8
@Component({
9
  selector: 'user-parent',
10
  template: `

11
    <h2>There are {{users.length}} registered users {{status}} now</h2>

12
    <user-child *ngFor="let user of users"

13
      [user]="user"

14
      [status]="status">

15
    </user-child>

16
  `
17
})
18
export class UserParentComponent {
19
  users: { id: number, name: string }[] = [
20
    { "id": 0, "name": "Chris" },
21
    { "id": 1, "name": "Dwayne" },
22
    { "id": 2, "name": "Eve" }
23
  ];
24
  status: string =  "online";
25
  
26
}
1
/* UserChildComponent.ts */
2
3
import { Component, Input } from '@angular/core';
4
 
5
// Input properties are adorned with @decorators

6
// user & status are input properties

7
 
8
@Component({
9
  selector: 'user-child',
10
  template: `

11
    <h2>{{user.name}}</h3>

12
    <p> id : {{user.id}} </p>

13
    <p> Status: {{status}} </p>

14
  `
15
})
16
export class UserChildComponent {
17
  @Input() user: { id: number, name: string };
18
  @Input() status: string;
19
}

Creating a React Component

The concept of components is deeply rooted in React, just as it is in Angular. Facebook calls React "a component-based library that lets you build interactive user interfaces". However, unlike Angular, React components are just JavaScript functions with an arbitrary number of inputs and an output. The code below shows a component defined using a JavaScript function and using an ES6 class.

1
// Writing components using JavaScript functions
2
3
function Welcome(props) {
4
  return <h1>Hello, {props.name}</h1>;
5
}
6
7
// Writing components using ES6 Class
8
9
class Welcome extends React.Component {
10
  render() {
11
    return <h1>Hello, {this.props.name}</h1>;
12
  }
13
}

Each React component accepts an arbitrary number of inputs, which are stored inside an object named props.

It also has a render method, and as the name suggests, this method determines what will be rendered when the component is invoked. Each component maintains an internal state (via this.state), and every time the state changes, the render function of that component is invoked again.

Language Features: TypeScript vs. ES6 and JSX

Angular applications are written in TypeScript, which is a superset of ECMA2015 and uses a transpiler to compile your strongly typed .ts file to a plain .js file. TypeScript offers language extensions that are designed to make writing in JavaScript easier, and it associates type information with JavaScript entities to enforce type checking and enhance the development workflow.

Some of the key features of TypeScript include optional static typing and support for interfaces, classes, and decorators. (Decorators are functions that are prefixed with @ and immediately followed by a class, parameter, or property.)

React also extends vanilla JS with some new language features. Let's dive into React, shall we? One of the most important language features in React is evident in this code sample.

1
function Tweet(props) {
2
  return(
3
  <div className="tweet">
4
        <img src="https://twitter.com/some-avatar.png" className="tweet__avatar" />
5
        <div className="tweet__body">
6
            <p>This is a tweet.</p>  
7
        </div>
8
      </div>
9
    );
10
}
11
    

Isn't this great? React lets you embed XML/HTML tags into your JavaScript file, and this is done through JSX, which offers syntax extension capability to JavaScript. Of course, this also means we have to use a transcompiler like Babel, which compiles our JSX code into the JavaScript that browsers can understand. The above code compiles down to this:

1
"use strict";
2
3
function Tweet(props) {
4
  return React.createElement(
5
    "div",
6
    { className: "tweet" },
7
    React.createElement("img", { src: "http://twitter.com/some-avatar.png", className: "tweet__avatar" }),
8
    React.createElement(
9
      "div",
10
      { className: "tweet__body" },
11
      React.createElement(
12
        "p",
13
        null,
14
        "This is a tweet."
15
      )
16
    )
17
  );
18
}

Although using JSX is recommended, you can stick with the much more verbose vanilla JavaScript React.createElement() syntax if you are against the idea of embedding HTML tags into JavaScript.

Type Checking in Angular vs. PropTypes in React

Static type checking is performed at compile time. The compiler warns you about potential type mismatches and detects certain errors that would otherwise go unnoticed. Additionally, defining a contract on a variable, a property, or the parameters of a function can result in more readable and maintainable code.

TypeScript and Type Safety

Variable and function declarations are made more expressive by declaring their data types. You can read more about the different primitive data types in the TypeScript documentation.

1
let isLoggedIn: boolean = false;
2
let id: number = 10;
3
let name: string = "Davis";
4
let list: number[] = [1, 2, 3];
5
6
enum Color {Red, Green, Blue};
7
let c: Color = Color.Red;
8
let bucket: any = 4;
9
bucket = "I can be a string";
10
bucket = false; // or a boolean

Defining the signature of an API using an interface makes the code less ambiguous and easier to comprehend. The interface serves as a quick start guide that helps you get started with code immediately and saves time otherwise spent on reading the documentation or the actual implementation of the library.

1
interface ButtonSettings {
2
    text: string;
3
    size?: { width: number; height: number; };
4
    color?: string;
5
}
6
7
function createButton(settings: ButtonSettings) { ... }
8
9
createButton({ text: 'Submit' }); // OK

10
createButton({ text: 'Submit', size: { width: 70, height: 30 }}); // OK

11
createButton({ text: 'Submit', color: 43); // Not OK: 43 isn't a string

12
createButton({ text: 'Submit', size: { width: 70 }); // Not OK: size needs a height as well

13
createButton({ color: 'Blue'}); // Not OK: 'text' member is required

The type keyword in TypeScript can be used to create an alias for a type. You can then create new types which are a union or intersection of these primitive types.

1
//Union Types

2
3
type Age = number | string;
4
function getAge (age: Age): string {
5
  return `You are ${age}!`;
6
}
7
8
let ofSusan: Age =21;
9
let ofTyler: Age = 'thirty one';
10
getAge(ofSusan); // You are 21!

11
getAge(ofTyler); // You are thirty one!

12
13
//Intersection Types

14
15
 interface Name{
16
  name(firstName: string, lastName: string): string;
17
}
18
19
interface Age {
20
  age(current: number): number;
21
}
22
// assign intersection definition to alias User

23
type User = Name & Age;
24
25
function createUser (testUser: User) {
26
  testUser.name("David","John");
27
  testUser.age(99);
28
  testUser.address(); //error 

Type Checking With React Prop Types

React has limited support for type checking because the underlying ES6 doesn’t support it. Nevertheless, you can implement type checking using the prop-types library developed by the React team. Type checking the props of a component to check whether it is a string can be done as shown below.

1
import PropTypes from 'prop-types';
2
//importing prop-types library

3
4
class Greeting extends React.Component {
5
  render() {
6
    return (
7
      <h1>Hello, {this.props.name}</h1>

8
      <P> My age is, {this.props.age} </h2>

9
    );
10
  }
11
}
12
13
Greeting.propTypes = {
14
  name: PropTypes.string;
15
  age: PropTypes.number;
16
};

But prop-types are not limited to strings, numbers, and booleans. You can do a lot more, as described in the prop-types documentation. However, if you take static type checking seriously, you should use something like Flow, which is a static type-checker library for JavaScript.

Scaffolding: Angular CLI vs. create-react-app

Starting a project from the ground up might seem fun initially. However, the process of setting up the directory structure, writing boilerplate code for components, and getting the application bootstrapped is a time-consuming and unproductive exercise when you're on a schedule. Your strategy should be to get on with your app as quickly as possible and focus on the actual development. Thanks to Google and Facebook, you have tools available to create and scaffold your applications with ease.

Setting up Angular-CLI for Angular and create-react-app for React is straightforward using npm.

1
// Angular CLI
2
3
$ npm install -g @angular/cli
4
5
// create-react-app
6
7
$ npm install -g create-react-app

Using Angular CLI

To create a new Angular application, you should use the following command:

1
$ ng new PROJECT-NAME
2
$ ng serve

But that's not it. The ng generate command lets you generate components, routes, pipes, directives, and services.

1
$ ng generate component Page
2
3
installing component
4
  create src\app\page\page.component.css
5
  create src\app\page\page.component.html
6
  create src\app\page\page.component.spec.ts
7
  create src\app\page\page.component.ts
8
  update src\app\app.module.ts

Angular CLI can do a lot more, like creating a build of your Angular app, commands for running unit tests, and end-to-end testing. You can read more about it on GitHub.

Using create-react-app

On the other hand, create-react-app is the officially supported way of creating a React app without any configuration files.

$ npm install -g create-react-app

This should create a functional React app with all the Babel and Webpack dependencies taken care of. You can start running the app on your browser using npm start.

You can find the scripts available for the React app in the package.json file.

1
  
2
  "scripts": {
3
    "start": "react-scripts start",
4
    "build": "react-scripts build",
5
    "test": "react-scripts test --env=jsdom",
6
    "eject": "react-scripts eject"
7
  }
8
}

Data Binding: Two-Way Binding vs. Unidirectional Binding

Data binding is a feature that enables synchronization of data between the application state (model) and the view. In a one-way data binding routine, any change in the state of the application automatically updates the view. On the contrary, two-way data binding binds together properties and events under a single entity: any modification of the model updates the view and vice versa.

Data Flow in React

In React, the properties are passed down from parent to child components, which is known as the unidirectional or top-down data flow. The state of a component is encapsulated and is not accessible to other components unless it is passed down to a child component as a prop: the state of a component becomes the prop of the child component.

1
class UserChild extends React.Component {
2
  render() {
3
 
4
    let userData = this.props.users.map( (user) => {
5
      return (<p> <strong>{user.id} </strong> : {user.name} </p>);
6
      });
7
   
8
    return (
9
        <div>
10
          <h2> Hello. The server is {this.props.status} </h2>

11
          {userData}
12
        </div>

13
    );
14
  }
15
}
16
17
class UserParent extends React.Component {
18
  constructor() {
19
    super();
20
    //State gets defined here

21
    this.state = {
22
      status: "Online"
23
    }
24
  }
25
  render() {
26
    return (
27
      <div>
28
       
29
        <UserChild users={this.props.users} status={this.state.status} />

30
      </div>

31
    );
32
  }
33
}
34
35
var USERS = [
36
    { "id": 0, "name": "Chris" },
37
    { "id": 1, "name": "Dwayne" },
38
    { "id": 2, "name": "Eve" }
39
];
40
41
42
ReactDOM.render(
43
  <UserParent users={USERS} />,

44
  document.getElementById('container')
45
);

But what if you need to propagate the data up through the component tree? This is done through child events and parent callbacks. The React documentation includes a good example that deals with such a scenario.

Data Binding in Angular

Data binding techniques available in Angular are among the unique features that make it stand out. Angular has out-of-the-box support for interpolation, one-way binding, two-way binding, and event binding.

Interpolation is the simplest way to bind your component property in the text between your HTML tags and attribute assignments.

<p>Welcome back {{currentUser.name}}!</p>

Property binding is similar to interpolation in the sense that you can bind the properties of your view elements to component properties. Property binding favors component communication and is identical to how props are passed down in React.

<img [src]="userImgUrl">

<user-child [user]="currentUser"></user-child>

Event bindings allow data to flow in the opposite direction, i.e. from an element to a component. Here, click is a target event, and on the right, we have the onSave() method that gets invoked when the event occurs.

<button (click)="onSave()">Save</button>

But the most important feature is the two-way binding using [(ngModel)]. This merges the property binding and event binding under one directive and is particularly useful with forms and input fields.

1
<div>
2
  <label>name: </label>
3
  <input [(ngModel)]="hero.name" placeholder="name">
4
</div>

Server-Side Rendering

Server-side rendering is a traditional rendering technique. Here, the server returns the whole HTML file upon request, and the browser is left with the simple job of displaying it to the user. Client-side rendering, on the other hand, returns a bare-bones HTML document, the stylesheet, and a JavaScript file. The JavaScript makes subsequent requests to render the rest of the website using a browser. React, Angular and all other modern JavaScript front-end libraries are good examples of client-side rendering. This is evident if you view the source of your Angular/React application.

But client-side rendering has the drawbacks that it doesn’t work great for SEO and that it returns incomplete HTML content when you share your link on social media sites. Angular has a solution called Angular Universal that takes care of making your app search engine friendly and social media friendly. It’s a library built by the Angular team, and using it is definitely favored.

Universal makes use of a pre-rendering technique where the whole website is rendered from the server first, and after a couple of seconds, the user is switched to the client-side rendering. Since all this happens under the hood, the user doesn’t notice anything different.

If you are using React with Redux, the Redux documentation has a good tutorial on setting up server rendering. You can also set up React to render from the server using the BrowserRouter and StaticRouter components available in the react-router library. You can read more about it in this Medium article. But if you are into performance and optimization, you can try next.js, which is a library for SSR in React.

React vs. Angular: Pros and Cons

Let's look at some of the general advantages and disadvantages of Angular and React:

  React Angular Winner
Mobile Apps React Native provides native-like performance with a similar platform to React Ionic is a WebView mobile app platform based on Angular React
App Speed React is fast to render with virtual DOM technology Angular has improved performance in recent years, but is still not as fast as React React
App Size React itself is very small, but your app size will depend on the external libraries you add Angular tends to produce heavier apps React
Server-Side Rendering React supports server-side rendering—the Redux library makes this easier

Angular also supports server-side rendering with Angular Universal

tie

Ease of Learning Core React has a simple structure and syntax that can be quickly mastered

Angular has a steeper learning curve with many novel concepts and structures

React

Project Setup create-react-app makes project setup easy

Angular CLI makes it easy to bootstrap a project.

tie

Structure and Architecture React doesn't provide much guidance out of the box on how to architect a scalable and maintainable app—you'll have to research this yourself

Angular provides an opinionated architecture—you won't have to reinvent the wheel

Angular

Routing You'll need a third-party library for routing—but React Router is very popular and is de facto part of React

Angular comes with support for routing built in

tie

HTML Templating React has the JSX syntax for integrating HTML with JavaScript code—things like loops and conditional rendering are handled with regular JavaScript

Angular uses HTML templates with special directives for tasks like loops and conditional rendering

tie

Dependency Injection React doesn't support dependency injection by default

Angular uses dependency injection from the ground up, making it easier to architect your app

Angular

Data Binding React's one-way data binding can be tricky to use at first

Angular's two-way data binding makes it easy to wire your data and components together

Angular

Should I Use Angular or React?

The development process is good with both React and Angular. Both technologies are powerful, and picking the ideal one comes down to personal preference. However, you must make decisions depending on your needs for functionality and usability, as well as the necessity to expedite development. The above pros and cons of each framework or library will help you in making a better decision.

Wrapping It Up

Comparing a full-blown, feature-rich framework to a robust UI library might not seem fair. However, they are advanced JavaScript technologies used to create interactive single-page applications, and in that regard, this article should help you decide on choosing one of them.

What are your thoughts on Angular vs. React? Do share them on the forum.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.