Tutorial
Intro
- It is a fronted javascript library
- Created by Facebook
- A tool for building UI Components
How it works?
- Creates a virtual DOM Memory
- only changes what needs to be changed
-To use react, we need npm and Node.js installed.
- For production we need to set up a react environment.
Setting up a react environment and run react
If you have npx and Node.js installed, you can create a React application by using create-react-app
cd my-react-app
npm start
Modify the react Application
Look in the my-react-app
directory, and you will find a src
folder. Inside the src
folder there is a file called App.js
, open it.
Classes:
A class is a type of function, but instead of using the keyword function
to initiate it, we use the keyword class
, and the properties are assigned inside a constructor()
method.
Create a class named "Model" which will inherit the methods from the "Car" class:
class Car {
constructor(name) {
this.brand = name;
}
present() {
return 'I have a ' + this.brand;
}
}
class Model extends Car {
constructor(name, mod) {
super(name);
this.model = mod;
}
show() {
return this.present() + ', it is a ' + this.model
}
}
const mycar = new Model("Ford", "Mustang");
mycar.show();
The super()
method refers to the parent class.
By calling the super()
method in the constructor method, we call the parent's constructor method and get access to the parent's properties and methods.
Arror Functions
- Allows us to write shorter function syntax.
hello = function() {
return "Hello World!";
}
With Arrow Function:
hello = () => {
return "Hello World!";
}
If the function has only one statement, and the statement returns a value, you can remove the brackets and the return
keyword:
hello = () => "Hello World!";
If you have parameters, you pass them inside the parentheses:
hello = (val) => "Hello " + val;
What About this
?
The handling of this
is also different in arrow functions compared to regular functions.
In short, with arrow functions there is no binding of this
.
In regular functions the this
keyword represented the object that called the function, which could be the window, the document, a button or whatever.
With arrow functions, the this
keyword always represents the object that defined the arrow function.
Variables
Now, with ES6, there are three ways of defining your variables: var
, let
, and const
If you use var
outside of a function, it belongs to the global scope.
If you use var
inside of a function, it belongs to that function.
If you use var
inside of a block, i.e. a for loop, the variable is still available outside of that block.
let
is the block scoped version of var
, and is limited to the block (or expression) where it is defined.
If you use let
inside of a block, i.e. a for loop, the variable is only available inside of that loop.
const
is a variable that once it has been created, its value can never change
Array Methods
One of the most useful in React is the .map()
array method.
The .map()
method allows you to run a function on each item in the array, returning a new array as the result.
In React, map()
can be used to generate lists.
const myArray = ['apple', 'banana', 'orange'];
const myList = myArray.map((item) => <p>{item}</p>)
Destructuring
To illustrate destructuring, we'll make a sandwich. Do you take everything out of the refrigerator to make your sandwich? No, you only take out the items you would like to use on your sandwich.
Destructuring is exactly the same. We may have an array or object that we are working with, but we only need some of the items contained in these.
Destructuring makes it easy to extract only what is needed.
Before:
const vehicles = ['mustang', 'f-150', 'expedition'];
// old way
const car = vehicles[0];
const truck = vehicles[1];
const suv = vehicles[2];
With destructuring:
const vehicles = ['mustang', 'f-150', 'expedition'];
const [car, truck, suv] = vehicles;
If we only want the car and suv we can simply leave out the truck but keep the comma:
const vehicles = ['mustang', 'f-150', 'expedition'];
const [car,, suv] = vehicles;
Spread Operator
The JavaScript spread operator (...
) allows us to quickly copy all or part of an existing array or object into another array or object.
const numbersOne = [1, 2, 3];
const numbersTwo = [4, 5, 6];
const numbersCombined = [...numbersOne, ...numbersTwo];
Combine these two objects:
const myVehicle = {
brand: 'Ford',
model: 'Mustang',
color: 'red'
}
const updateMyVehicle = {
type: 'car',
year: 2021,
color: 'yellow'
}
const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}