Menu

ReactJS is a Javascript library that is built on the premise of declarative and reusable components. It’s the view layer in MVC and can be used with other frameworks or libraries to build applications.

one-way data binding: example – when you type in an input field, it doesn’t directly change the state of that component. Instead, it updates the data model, which causes the UI to be updated.

virtual DOM: A virtual representation of the real DOM that React uses to intelligently re-render each component. Each time the data changes in the app, a new Virtual DOM is created, and the differences between the new Virtual DOM and the previous Virtual DOM will be updated in the real DOM. Read more.

JSX: XML/HTML-like syntax used by React that extends ECMAScript so that XML/HTML-like text can co-exist with JavaScript/React code. (use Babel to transpile JSX and ES6 into Javascript). JSX expressions are shorthand for ‘React.createElement’.

Components:

A React app is a tree of nested components.

Props and state control each component.

Components must implement render.

Use class components for stateful components.

Don’t use class for stateless components.

Events:

Events are created inside components and are added to their corresponding render function. (e.g. onListItemHover event above).

Props:

Props contain information set by the parent component and can be passed to the child component(s). It’s the equivalent of $scope in Angular).

When a component is rendered, it can access its props using this.props.

Props are immutable.

State:

State is what makes your app interactive.

In general, you should initialize state in the constructor, and then call setState when you want to change it.

State is mutable.

State should be held at the top-most component that needs access to it.

Components manage their own state.

Initial values can be set for state.

State can be passed from parent components to child components as props.

Example: Component A is parent of Component B. When component B is clicked on, it updates state on component A. In order for this to happen:

Define a function that updates state on component A.

Pass the function as a prop to component B.

Invoke the function when B is clicked on.

Lifecycle Methods:

React components have a lifecycle, and you’re able to access specific phases of that lifecycle to update certain parts of each component.

Component creation:

componentWillMount: called before the render method is executed.

componentDidMount: called after render method is executed.

example: You’re creating an application that streams music from the SoundCloud API as soon as a client connects to your website. You would use componentDidMount to make that Ajax request to SoundCloud to ensure you have the song data you need once the application renders.

Angular is an MVW (model-view-whatever) framework for properly architected single-page applications. It’s opinionated, declarative, has two-way binding, and allows separation of presentation and business logic.

In example above, controller is not part of a component so to access the attributes, you have to use $scope. If the controller is within a component, you have to use ‘this’ (and $ctrl in template).

Scope:

In Angular 1, you access scope using $scope.

In Angular 2, you access scope using this (because $scope and controllers don’t exist) and $ctrl. (It’s similar to props in React)

Factories/Services:

Factories and services are objects that you can register with Angular so their data and behavior can be shared among different parts of the app. (e.g. registering an API with Angular so it can be accessed everywhere in the app)

The main difference between factories and services is that services are instantiated with the ‘new’ keyword so you do not need to return the object.

Services allow the use of ES6 classes because they are constructor functions.

To solve this problem recursively, think of the game as a tree and you want to traverse all possible solutions depth-first. That means going all the way down one branch first before moving back up to evaluate the previous level(s).

When a function is called as a method of an object, that function’s this argument is set to the object the method is called on. That object is called the receiver of the function call.

Oftentimes, the receiver gets lost when we invoke a method as a function. This happens particularly often when passing a method as a callback to another function. (example below – ‘this’ context is lost when assigning method to greet)

permanently tie a function’s ‘this’ argument to a specific value. It creates a new bound function that calls the original function with the provided ‘this’ argument, no matter how that bound function is called.

Within the constructor of a class, this refers to the newly created object. Within a method, however, this might refer to another value if the method is called as an ordinary function. Just like any other object method, class methods can lose their intended receiver this way.