Key Points
References
Reference_description_with_linked_URLs_______________________ | Notes______________________________________________________________ |
---|---|
https://programmingwithmosh.com/react/react-vs-angular/ | compare angular and react - p1 |
Key Concepts
Compare Angular and React
https://programmingwithmosh.com/react/react-vs-angular/
Angular is a JavaScript framework written in TypeScript. It was developed and is maintained by Google, and is described as a “Superheroic JavaScript MVWFramework”
React is a JavaScript library developed and maintained by Facebook.
Angular is a full-fledged MVC framework and React is merely a JavaScript Library (just the view)
Angular provides the following “out of the box”:
- Templates, based on an extended version of HTML
- XSS protection
- Dependency injection
- Ajax requests by @angular/HTTP
- Routing, provided by @angular/router
- Component CSS encapsulation
- Utilities for unit-testing components.
- @angular/forms for building forms
React needs Redux to manage state. With React 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.
React provides the following “out of the box”:
- Instead of classic templates, it has JSX, an XML-like language built on top of JavaScript
- XSS protection
- No dependency injection
- Fetch for Ajax requests
- Utilities for unit-testing components
Some popular libraries to add functionality are:
- React-router for routing
- Redux or MobX for state management
- Enzyme for additional testing utilities
DOM vs Virtual DOM
React's virtual DOM has fast updates to the specific items in the DOM
Angular updates the full DOM until all the changed items have been updated - slower
Templates
React decided to combine UI templates and inline JavaScript logic, which no company had ever done before. The result is called “JSX”
React uses something called a component, which contains both the markup AND logic in the same file. It also uses an XML-like language that allows you to write markup directly in your JavaScript code. JSX is a big advantage for development, because you have everything in one place, and code completion and compile-time checks work better.
const name = 'Josh Perez';
const element = <h1>Hello, {name}</h1>;
Angular uses templates that are enhanced HTML with Angular directives (“ng-if” or “ng-for”). React only requires knowledge of JavaScript, but with Angular, you must learn its specific syntax.
Components
Both React and Angular are both component-based. A component receives an input, and after some internal logic returns a rendered UI template (a sign-in form or a table for example) as output. Components should be easy to reuse within other components or even in other projects
State management
The UI is described by the component at a given point in time. Then, the framework re-renders the entire UI of the component when data changes. This ensures that the data is always up to date.
To handle state in React, Redux is often used as the solution. In Angular, you may not need Redux. But, if your application becomes large enough, chances are that you will. Some developers, including me, opt to use MobX instead of Redux. MobX has more “magic” (things automatically done for you behind the scenes) and I personally prefer it.
Data Binding
A large difference between React and Angular is one-way vs. two-way binding. Angular uses two-way binding. For example, if you change the UI element (a user input) in Angular, then the corresponding model state changes as well. Additionally, if you change the model state, then the UI element changes – hence, two-way data binding.
React does not update the model when the UI changes - 1 way data binding
React only has one-way binding. First, the model state is updated, and then it renders the change in the UI element. However, if you change the UI element, the model state DOES NOT change. You must figure that out for yourself. Some common ways are through callbacks or state management libraries
Typescript vs Javascript Flow
React uses JavaScript, a dynamically-typed language (which means you don’t have to define the variable’s type). Because many developers already know and love JavaScript, this can be seen as a pro. With ES6, you have modules, classes, spread operators, arrow functions, template literals and more. It allows developers to write declarative code while having the characteristics of a true OOP language (that is, including class-based structure).
Conversely, if you want to use Angular you’ll need to get comfortable with TypeScript. TypeScript is a statically typed language, which means you must define the variable’s type (string, number, array, etc). It is simply a transpiler that compiles TypeScript to JavaScript code, which also means you can write ES5 code in a TypeScript file.
TypeScript’s purpose is to ensure a smooth transition for programmers with an Object Oriented Programming (OOP) background. TypeScript was also released in the period of ES5, and during that time, ES5 was not a class-based OOP language.
Flow adds type checking to Javascript
You should also probably know that you could use Flow to enable type checking within your React project. It’s a static type-checker developed by Facebook for JavaScript. If you so choose, you can also use TypeScript in your React project (although it isn’t natively included).
// JavaScript (ES6)
function getName(name, age){
return name + age;
}
// TypeScript
function getName(name: string, age: number){ // <-- static typed!
return name + age;
}
================================
Classes in Javascript vs Typescript
// JavaScript (ES6)
class Greeter {
constructor(message) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("JavaScript!");
greeter.greet()
// Hello, JavaScript!
================================
// TypeScript
class Greeter { // <-- static typed!
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("TypeScript!");
greeter.greet()
// Hello, TypeScript!
Mobile Apps
Angular and React both offer solutions to create mobile applications.
Ionic is a framework for developing hybrid mobile applications. It uses a Cordova container that is incorporated with Angular. Ionic provides a robust UI component library that is easy to set up and develop hybrid mobile applications with. However, the resulting app on a device is simply a web app inside of a native web view container. Because of this, the apps can be slow and laggy.
React Native, on the other hand, is a platform developed by Facebook for creating truly native mobile applications using React. The syntax is slightly different, but there are much more similarities than differences. Unlike Ionic, which is simply a glorified web app, React Native produces a truly native UI. It also allows you to create your own components and bind them to native code written in Objective-C, Java, or Swift.
Testing frameworks
Jest is used by Facebook to tests its React code. It is included in every React project and requires zero configuration to use. It also includes a powerful mocking library. Many times Jest is used in combination with Enzyme (a JavaScript testing utility used at Airbnb).
Jasmine is a testing framework that can be used in Angular. Eric Elliott says that Jasmine “results in millions of ways to write tests and assertions, needing to carefully read each one to understand what it’s doing”. The output, in my opinion, is also very bloated and difficult to read. Here are some educational articles on the integration of Angular with Karma and Mocha.
Learning process
React
The first thing you’ll learn in React is JSX. It may seem awkward to write at first, but it doesn’t add much complexity. You’ll also need to learn how to write components, manage internal state, and use props for configuration. You don’t need to learn any new logical structures or loops since all of this is plain JavaScript.
Once you’re done learning the basics, you’ll need to learn a routing library (since React doesn’t come with one). I recommend react router v4. Next comes state management with Redux or MobX. I’ve already touched upon this subject, so I’ll skip this. Once you’ve learned the basics, a routing library, and state management library, you’re ready to start building apps!
Angular
Angular has many topics to learn, starting from basic ones such as directives, modules, decorators, components, services, dependency injection, pipes, and templates. After that, there are more advanced topics such as change detection, zones, AoT compilation, and Rx.js.
The entry barrier for Angular is clearly higher than for React. The sheer number of new concepts is confusing to newcomers. And even after you’ve started, the experience might be a bit rough since you need to keep in mind things like Rx.js subscription management and change detection performance.
It may seem like React has a lower barrier for entry, and I would most certainly have to agree. However, that doesn’t mean that React is “better”. I encourage you to try both React and Angular to see which one you personally prefer.
what frameworks are developers interested in? 2018
Summary
Angular:
- Is a full framework
- Has a Regular DOM, which renders updates slower than React’s Virtual DOM
- The rendered JavaScript and HTML maintains a physical separation
- Utilizes Components: emerging web components standard
- Data Binding: two-way
- You must use TypeScript
- Mobile: Ionic and Cordova are slower than React Native
- Testing: Jasmine & Mocha
- Learning Curve is higher, but once you understand it you have an entire MVC framework
- Scalability: easy to scale
- Popularity: dropped since AngularJS (Angular 1)
- Open source: GitHub stars: 40,963 / Contributors: 732 / Issue: 2,162
- Size: larger, resulting in longer load times and performance on mobile
- Used on: Google, Nike, Forbes, Upwork, General Motors, HBO, Sony
React:
- Just a small view library
- Has a Virtual DOM, which renders updates faster than Angular’s Regular DOM
- Uses JSX, which combines markup and logic in the same file (making components easier to read)
- Components: emerging web components standard
- Data Binding: one-way
- You Can use ES6/7 JavaScript, although you can use Typescript or Flow if you so choose
- Mobile: React Native is faster than Angular’s solutions
- Testing: Jest & Enzyme
- Learning Curve is lower, but you only get the view. Because of this, you’re going to have to learn a slew of 3rd party libraries. Ex. State management (Redux or MobX), Asynchronous calls (react-promise, react-thunk, or react-saga), etc.
- Scalability: is more testable, so also easy to scale
- Popularity: has increased exponentially
- Open source: GitHub stars: 111,927 / Contributors: 1,242 / Issues: 287
- Size: smaller than Angular, so a bit faster
- Used on: Facebook, Airbnb, Uber, Netflix, Instagram, Whatsapp, Dropbox
- React Fiber will increase the speed of React dramatically
Mosh's React Course
https://codewithmosh.com/p/mastering-react
$29 one time payment for course
Potential Value Opportunities
Potential Challenges
Candidate Solutions
Step-by-step guide for Example
sample code block