- ReactJS
- Javascript for ReactJS
- Arrow Function
- Destructuring Assignment
- Array Destructuring
- Examples
- Object Destructuring
- Basic assignment
- Assignment separate from declaration
- Assigning to new variable names
- Default values
- Assigning to new variable names and providing default values
- Unpacking properties from objects passed as a function parameter
- Setting a function parameter's default value
- Nested object and array destructuring
- Computed object property names and destructuring
- Rest in Object Destructuring
- Invalid JavaScript identifier as a property name
- Combined Array and Object Destructuring
- The prototype chain is looked up when the object is deconstructed
- Spread Operator Syntax (...)
- Classes
- ES/JS Modules
- Exporting Classes From a Module
- References
ReactJS is a library to make UI. Nothing more!
npx create-react-app my-app
cd my-app
npm start
npm start
- Runs the app in the development mode.
- By default, the application runs in localhost port 3000 with the address http://localhost:3000.
npm run build
- Builds the app for production to the
build
folder. - It correctly bundles React in production mode and optimizes the build for the best performance.
- Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.
- Components are like JavaScript functions.
- They accept arbitrary inputs (called props) and return React elements describing what should appear on the screen.
- We call such components function components because they are literally JavaScript functions.
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
Arrow Function Component Syntax
const Welcome = props => <h1>Hello, {props.name}</h1>
ES6 Class Syntax
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
- Component name must start with a capital letter.
React treats components starting with lowercase letters as DOM tags. For example,
<div />
represents an HTML div tag, but<Welcome />
represents a component
- Always close the tag for React components e.g;
<Welcome>OR</Welcome>
<Welcome />
- Use
camelCase
for HTML attributes.
<Welcome onClick="" />
- Use
className
attribute instead ofclass
attribute for CSS class selector.
<Welcome className="" />
- Component must have a root element. It can be a
div
or Fragment<>
Event handling of React Elements is the same as DOM elements with following syntax differences.
- React events are named using camelCase, rather than lowercase.
- With JSX, you pass a function as the event handler, rather than a string.
<button onclick="activateLasers()">
Activate Lasers
</button>
<button onClick={activateLasers}>
Activate Lasers
</button>
Other differences are
- An event handler is supposed to be either a function or a function reference.
<button onClick={activateLasers}>
Activate Lasers
</button>
<button onClick={() => { console.log('Hello World') }}>
Activate Lasers
</button>
- You cannot return false to prevent default behavior in React. You must call
preventDefault
explicitly.
Before learning about hooks, make sure you understand the concept of Destructing Assignment Syntax
- Hooks are a new addition in React 16.8.
- They let you use state and other React features without writing a class.
- Hooks are functions that let you hook into React state and lifecycle features from function components.
- React provides a few built-in Hooks like useState.
- You can also create your own Hooks to reuse stateful behavior between different components.
Hooks are JavaScript functions, but they impose two additional rules
- Only call Hooks at the top level. Don’t call Hooks inside loops, conditions, or nested functions.
- Only call Hooks from React function components or custom hooks. Don’t call Hooks from regular JavaScript functions.
const App = () => {
// these are ok
const [age, setAge] = useState(0)
const [name, setName] = useState('Juha Tauriainen')
// Conditiona Example: this does not work!
if ( age > 10 ) {
const [foobar, setFoobar] = useState(null)
}
// Loop Example: Not good
for ( let i = 0; i < age; i++ ) {
const [rightWay, setRightWay] = useState(false)
}
// Nested Function: this is also illegal
const notGood = () => {
const [x, setX] = useState(-1000)
}
return (
//...
)
}
-
It has a shorter syntax than the standard function.
-
It does not bind their own
this
. -
Arrow functions are best suited for non-method functions.
Example Syntax
(param1, param2) => { statements }
If the function has only one param, you can emit the ()
around the paramters and if it has only one statment in the method, you can emit {}
in method body. And when you don't put {}
brances, it will return that expression result.
param => statment
(param) => {return statmenet }
// Above 2 functions are equal.
If there is no parameter required for a function, ()
is required.
() => statmenet
() => { expressions }
() => { return expression }
Example Methods
const multiply = (x*y) => { return x*y }
const multiply = (x*y) => return x*y
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
-
Imagine you have an array and you would like to assign each item from an array to local variables.
-
The destructuring assignment uses similar syntax, but on the left-hand side of the assignment to define what values to unpack from the sourced variable.
-
In the destructuring assignment, each index on the left-hand side correspond to respective index in an array on right-hand side of assignment.
const [firstElement, secondElement] = list;
// is equivalent to:
// const firstElement = list[0];
// const secondElement = list[1];
const x = [1, 2, 3, 4, 5];
const [y, z] = x;
console.log(y); // 1
console.log(z); // 2
let a, b;
[a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2
If an array has N
elements and the number of variables specified on the left-hand side of the assignment is greater than N, only the first N variables are assigned values.
const foo = ['one', 'two'];
const [red, yellow, green, blue] = foo;
console.log(red); // "one"
console.log(yellow); // "two"
console.log(green); // undefined
console.log(blue); //undefined
A variable can be assigned a default, in the case that the value unpacked from the array might be undefined
.
let a, b;
[a=5, b=7] = [1];
console.log(a); // 1
console.log(b); // 7
let a = 1;
let b = 3;
[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1
Destructuring can make working with an array return from a function or passsing to a function more concise.
function f() {
return [1, 2];
}
let a, b;
[a, b] = f();
console.log(a); // 1
console.log(b); // 2
function f([a,b]) {
console.log(a); // 1
console.log(b); // 2
}
f([1,2]);
You can ignore return values that you're not interested in.
function f() {
return [1, 2, 3];
}
const [a, , b] = f();
console.log(a); // 1
console.log(b); // 3
You can also ignore all returned values.
[,,] = f();
When destructuring an array, you can unpack and assign the remaining part of it to a variable using the rest pattern:
const [a, ...b] = [1, 2, 3];
console.log(a); // 1
console.log(b); // [2, 3]
const user = {
id: 42,
isVerified: true
};
const {id, isVerified} = user;
console.log(id); // 42
console.log(isVerified); // true
let a, b;
({a, b} = {a: 1, b: 2});
The parentheses ( ... ) around the assignment statement are required when using object literal destructuring assignment without a declaration.
const o = {p: 42, q: true};
const {p: foo, q: bar} = o;
console.log(foo); // 42
console.log(bar); // true
const {p: foo} = o
takes from the object o
the property named p
and assigns it to a local variable named foo
.
A variable can be assigned a default, in the case that the value unpacked from the object is undefined
.
const {a = 10, b = 5} = {a: 3};
console.log(a); // 3
console.log(b); // 5
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];
// Output = [1,2,3,4,5,6]
The spread operator is often used in combination with destructuring
.
const numbers = [1, 2, 3, 4, 5, 6];
const [one, two, ...rest] = numbers;
// one = 1
// two = 2
// rest = 3,4,5,6
We can use the spread operator with objects too. Below is an example of how we can combine 2 objects.
const myVehicle = {
brand: 'Ford',
model: 'Mustang',
color: 'red'
}
const updateMyVehicle = {
type: 'car',
year: 2021,
color: 'yellow'
}
const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}
// Output object will be like this
{
brand: 'Ford',
model: 'Mustang',
color: 'yellow',
type: 'car',
year: 2021,
}
- To declare a class, you use the
class
keyword with the name of the class.
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
// unnamed
let Rectangle = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
// output: "Rectangle"
// named
let Rectangle = class Rectangle2 {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
// output: "Rectangle2"
-
In programming, you try to break down your problems into small chunks and spread them over multiple files so that you can share them across projects and within projects.
-
In js, when you create a file, it's called a
module.
-
To use functions, classes, variables, and constants in other files, you need to know how to export them so that other modules can import them and use them.
-
You can export functions, var, let, const, and classes.
-
You put the
export
keyword before the functions, var, let, const and classes. -
You can't use export inside a function.
-
A more convenient way of exporting all the items you want to export is to use a single export statement at the end of your module file, followed by a comma-separated list of the features you want to export wrapped in curly braces
export const name = 'square'; // Constant
export function draw() {} // Function
export { name, draw, reportArea, reportPerimeter }; // more convenient syntax at the end of file
-
Above method of exporting is called named exports.
-
We have another method of exporting, called default export and below is the example syntax. Note the lack of curly braces.
-
Only one default export allowed per module.
export default randomSquare;
export default function(ctx) { }
-
Once you've exported some features out of your module, you need to import them where you wanted to use them.
-
You cannot change the variable that was imported, but you can still modify properties similar to
const
. -
To import, you use the
import
statement, followed by acomma-separated list
of the features you want to import wrapped incurly braces
, followed by the keywordfrom
, followed by thepath to the module file
.
import { name, draw, reportArea, reportPerimeter } from './modules/square.js';
-
Importing default export modules syntax.
-
Note the lack of curly braces. This is because there is only one default export allowed per module.
import randomSquare from './modules/square.js';
import {default as randomSquare} from './modules/square.js'; // expanded version of above line
-
Inside your import and export statement's curly braces, you can use the keyword
as
along with a new feature name, to change the identifying name you will use for a feature inside the top-level module. -
You need this feature because it's possible that more than 1 module exported with the same name.
-
Since you can't change the module of the third party, it's generally a good idea not to change the name of your own modules too and change names on the import side.
// inside module.js
export {
function1 as newFunctionName,
function2 as anotherNewFunctionName
};
// inside main.js
import { newFunctionName, anotherNewFunctionName } from './modules/module.js';
// inside module.js
export { function1, function2 };
// inside main.js
import { function1 as newFunctionName,
function2 as anotherNewFunctionName } from './modules/module.js';
- So far, when we import from a module, we import individual items from a module and use them e.g;
import { name, draw, reportArea, reportPerimeter } from './canvas.js';
name();
- Instead of importing all of them individually, you can import all of them in an
object
and then use feature of a module as functions or variables of a given object.
import * as Canvas from './canvas.js';
Canvas.name()
class Square {
constructor(ctx, listId, length, x, y, color) { }
draw() { }
}
export { Square };
import { Square } from './square.js';
let square1 = new Square(myCanvas.ctx, myCanvas.listId, 50, 50, 100, 'blue');
square1.draw();
square1.reportArea();
square1.reportPerimeter();