Mern
Mern
});
const sample_server = http.createServer(app);
say(); // 'Hi'
say(undefined); // 'Hi'
say('Hello'); // 'Hello'
Passing undefined arguments
function createDiv(height = '100px', width = '100px', border = 'solid 1px red') {
let div = document.createElement('div');
div.style.height = height;
div.style.width = width;
div.style.border = border;
document.body.appendChild(div);
return div;
}
createDiv(); //function uses the default values for the parameters.
createDiv(undefined,undefined,'solid 5px blue'); //createDiv(undefined,undefined,'solid 5px blue');
Evaluating default parameters
JavaScript engine evaluates the default arguments at the time
you call the function
function put(toy, toyBox = []) {
toyBox.push(toy);
return toyBox;
}
console.log(put('Toy Car'));
// -> ['Toy Car']
console.log(put('Teddy Bear'));
// -> ['Teddy Bear'], not ['Toy Car','Teddy Bear']
Using other parameters in default values
function add(x = 1, y = x, z = x + y) {
return x + y + z;
}
console.log(add()); // 4
rest
ES6 provides a new kind of parameter so-called rest parameter that has a prefix of
three dots (...). A rest parameter allows you to represent an indefinite number of
arguments as an array
function fn(a,b,...args) {
//...
}
• The last parameter (args) is prefixed with the three-dots ( ...). It’s called a rest
parameter ( ...args).
fn(1, 2, 3, "A", "B", "C"); args array stores [3,'A','B','C']
• fn(1,2); args array []
Set
ES6 provides a new type named Set that stores a collection of
unique values of any type. To create a new empty Set
let setObject = new Set();
The Set constructor also accepts an optional iterable object.
If you pass an iterable object to the Set constructor, all the
elements of the iterable object will be added to the new set:
console.log(x); // 70
console.log(y); // 80
console.log(z); // undefined
classes
class Person {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
}
Promises
Promises are used to handle asynchronous operations in
JavaScript.
They can handle multiple asynchronous operations easily and
provide better error handling than callbacks and events.
Promise has four states:
fulfilled: Action related to the promise succeeded
rejected: Action related to the promise failed
pending: Promise is still pending i.e. not fulfilled or rejected yet
settled: Promise has fulfilled or rejected
A promise can be created using Promise constructor.
Syntax
• var promise = new Promise(function(resolve, reject){
//do something
});
• Parameters
– Promise constructor takes only one argument which is a callback
function (and that callback function is also referred as anonymous
function too).
– Callback function takes two arguments, resolve and reject
– Perform operations inside the callback function and if everything
went well then call resolve.
– If desired operations do not go well then call reject.
The fulfilled state indicates that the asynchronous operation was
completed successfully:
Initialization: This is the stage where the component is constructed with the given Props
and default state. This is done in the constructor of a Component Class.
Mounting: Mounting is the stage of rendering the JSX returned by the render method
itself.
Updating: Updating is the stage when the state of a component is updated and the
application is repainted.
Unmounting: As the name suggests Unmounting is the final step of the component
lifecycle where the component is removed from the page.
• Mounting – Birth of your component
• Update – Growth of your component
• Unmount – Death of your component
render()
The render() method is the most used lifecycle method. You
will see it in all React classes. This is because render() is the
only required method within a class component in React.
Example
The render() method returns JSX that is displayed in the UI. A render() can also return a null if
there is nothing to render for that component.
You should not setState() (modify the state) inside
the render() method as it will result in an infinite loop.
• The constructor is where the initial state and the values are
set in a React component. This method is called before the
component is mounted. The purpose of the constructor is
to,
• Initialize the state
• Bind methods to the component
constructor(props) {
super(props); // Initialize state
this.state = {
currentIndex: 0,
}; // Bind methods
this.addToCart = this.addToCart.bind(this);
}
componentDidMount()
• This is the first method that will be called when the component gets
an update. The use case of this method is to update the state based
on prop changes when props aren’t sufficient to be directly used.
Let’s look at this example.
• static getDerivedStateFromProps(props, state)
{
return {favoriteColor: props.favoriteColor };
• This method is always called right after a component render and has updated
the DOM. It receives two values as arguments,
• prevProps — Props of the component before the update
• prevState — State of the component before the update
• componentDidUpdate = (prevProps) => {
const { currentUser } = this.props;
if (prevProps.currentUser !== currentUser) {
this.setAddress();
this.setPaymentOption();
}
}
componentWillUnmount()
constructor() {
super();
this.state = { counter: 1 };
}
• In the above code, the state 'counter' is set to 1
and 'counter' would be accessed as this.state.counter.
When an event occurs in the component, we will update the state of the component using
the setState() method inside the event handler method as shown below:
• Redux is an open-source JavaScript library used to manage application state. React uses Redux
for building the user interface.
• It allows React components to read data from a Redux Store, and dispatch Actions to
the Store to update data. Redux helps apps to scale by providing a sensible way to manage state
through a unidirectional data flow model.
• Redux is not a framework. Redux can be used with any other View library but mostly used with
React.
The main concepts of Redux architecture are:
• Store: The place where the entire state of the application is stored
• Actions: JS objects which encapsulate the events triggered within the application.
• Reducers: Pure functions that modify the state of the application according to the actions
triggered.
With Redux, we can make state data independent of the components, and when needed, a component
• The mapStateToProps() takes the current state of the application as an argument and
returns an object that contains the count property of the state.
• It is also possible to import an external module into a Node application, using NPM (Node Package
Manager).
• NPM provides a command-line interface "npm" to work with the NPM repository, which comes bundled
with Node. This tool can be used to install, update, or uninstall any package through NPM.