These notes will serve as a review of the syntax and functionality of React.js.
β This first file will review some important JS knowledge
ES6 Classes
Very simple, we can define classes in javascript (and typescript) and establish inheritance:
ES6 Arrow Functions
These simplify writing functions, especially when we are defining a function as we are passing it to another function. Whatβs worth noting here is the differences that occur if we use arrow functions for defining class methods:
The above this refers to the object which has called on the function, so window and button, respectively. This makes the output of the two functions different. Now, replacing the changeColor function with an arrow function changes things:
For the this above, they both refer the the Header object. So, both functions now have this referring to the same thing.
ES6 Variables
There are three ways of defining variables: let, var, and const
β Var
Outside of function, global scope
Inside function, local scope
Inside of a block (perhaps a loop), still available outside of block
β Let
Limited to the block scope, local
β Const
Limited to block scope, canβt change the value it references through it, but, that value can still change
ES6 Array Methods
Many useful array methods out there, here, letβs go over some of the more important ones to know. Others, you can just reference when needed.
β .map()
This allows us to map a function to each element of an array, so, passing each array element to the function, one at a time. Here is an example:
ES6 Destructuring
A very useful mechanism is the ability to destructure. This applies to many data structures:
β Array
β Objects
ES6 Spread Operator
This is the β¦ that you frequently see. The JavaScript spread operator (...
) allows us to quickly copy all or part of an existing array or object into another array or object.
β Arrays
Above, we have stored one = 1, two = 2, and rest = [3,4,5,6].
β Objects
ES6 Ternary Operator
Faster way to express logic gates.
Syntax:Β condition ? <expression if true> : <expression if false>