Shany  Jenkins

Shany Jenkins

1613712180

All Lifecycles in Class Components and Similar in Functional Components

Hi, today I’m going to explain the whole lifecycle of the class component also the same in functional components.

What you will learn after reading this article:

  • Execution of lifecycles in Class Components and Functional Components from the beginning to the end

  • How to implement Class Components lifecycles in Functional Components

  • Proper use of lifecycles in the component according to different modes

  • Lifecycle differences in Class Component and Functional Component

  • Very practical tips about where to call the components and their impact on the implementation of lifecycles

  • And other important and practical cases …

So let’s get started.

At first, we want to answer the question, what is the lifecycle of a component?

The lifecycle of a component in virtually all phases of its implementation from zero to one hundred until the component is fully rendered.

Each phase is suitable for a specific operation and events occur in it. So you should use the right life cycle in your component according to your needs, otherwise, it will cause errors and problems in your program.

So now that we understand these things, we are about on to introducing and explaining lifecycles.

The lifecycles of React components are divided into the following 3 sections, each of which will be described below:

1. Mounting: In this step, your initial states and data are initialized

2. Updating: In the update section, you can access your states and data, and you can perform update operations on it

3. Unmounting: And in the last section you can write the operations you want to do before unmounting of a component, such as clearing a SetInterval

#react-lifecycle #react #class-component #reactjs

What is GEEK

Buddha Community

All Lifecycles in Class Components and Similar in Functional Components
Shany  Jenkins

Shany Jenkins

1613712180

All Lifecycles in Class Components and Similar in Functional Components

Hi, today I’m going to explain the whole lifecycle of the class component also the same in functional components.

What you will learn after reading this article:

  • Execution of lifecycles in Class Components and Functional Components from the beginning to the end

  • How to implement Class Components lifecycles in Functional Components

  • Proper use of lifecycles in the component according to different modes

  • Lifecycle differences in Class Component and Functional Component

  • Very practical tips about where to call the components and their impact on the implementation of lifecycles

  • And other important and practical cases …

So let’s get started.

At first, we want to answer the question, what is the lifecycle of a component?

The lifecycle of a component in virtually all phases of its implementation from zero to one hundred until the component is fully rendered.

Each phase is suitable for a specific operation and events occur in it. So you should use the right life cycle in your component according to your needs, otherwise, it will cause errors and problems in your program.

So now that we understand these things, we are about on to introducing and explaining lifecycles.

The lifecycles of React components are divided into the following 3 sections, each of which will be described below:

1. Mounting: In this step, your initial states and data are initialized

2. Updating: In the update section, you can access your states and data, and you can perform update operations on it

3. Unmounting: And in the last section you can write the operations you want to do before unmounting of a component, such as clearing a SetInterval

#react-lifecycle #react #class-component #reactjs

Yashi Tyagi

1617449307

CA Classes - Best CA Classes Online

Chartered Accountancy course requires mental focus & discipline, coaching for CA Foundation, CA Inter and CA Finals are omnipresent, and some of the best faculty’s classes have moved online, in this blog, we are going to give the best way to find online videos lectures, various online websites provide the CA lectures, Smartnstudy one of the best site to CA preparation, here all faculty’s video lecture available.

check here : ca classes

#ca classes online #ca classes in delhi #ca classes app #ca pendrive classes #ca google drive classes #best ca classes online

Vincent Lab

Vincent Lab

1605017502

The Difference Between Regular Functions and Arrow Functions in JavaScript

Other then the syntactical differences. The main difference is the way the this keyword behaves? In an arrow function, the this keyword remains the same throughout the life-cycle of the function and is always bound to the value of this in the closest non-arrow parent function. Arrow functions can never be constructor functions so they can never be invoked with the new keyword. And they can never have duplicate named parameters like a regular function not using strict mode.

Here are a few code examples to show you some of the differences
this.name = "Bob";

const person = {
name: “Jon”,

<span style="color: #008000">// Regular function</span>
func1: <span style="color: #0000ff">function</span> () {
    console.log(<span style="color: #0000ff">this</span>);
},

<span style="color: #008000">// Arrow function</span>
func2: () =&gt; {
    console.log(<span style="color: #0000ff">this</span>);
}

}

person.func1(); // Call the Regular function
// Output: {name:“Jon”, func1:[Function: func1], func2:[Function: func2]}

person.func2(); // Call the Arrow function
// Output: {name:“Bob”}

The new keyword with an arrow function
const person = (name) => console.log("Your name is " + name);
const bob = new person("Bob");
// Uncaught TypeError: person is not a constructor

If you want to see a visual presentation on the differences, then you can see the video below:

#arrow functions #javascript #regular functions #arrow functions vs normal functions #difference between functions and arrow functions

Shahab Uddin

Shahab Uddin

1588449088

React Functional Components, Class Components and Pure Component

In this article we will learn about the different types of react components, and what are the differences in between the components (functional, class and pure components).

React components let you split the UI into independent, reusable pieces, and think about each piece in isolation. React components can be defined by subclassing React.Component or React.PureComponent as per react official doc.

Functional Components

A Functional component is the simplest way to define a component in ReactJs, it’s a simple javascript function which returns a React element. Functional components are also known as stateless components.

Let’s understand with an example of a Functional Component

import React from 'react';
 function App() {
  const greeting = 'Hello Functional Component!';
 return <h1>{greeting}</h1>;
}
 export default App;

React functional component with props
In React, props are just like HTML attributes for the components, these are used to pass some information from one component to another.

import React from 'react';
 function App() {
  const greeting = 'Hello Functional Component!';
 return <Headline value={greeting} />;
}
function Headline(props) {
  return <h1>{props.value}</h1>;
}
 export default App;

React functional component with ES6 arrow function

import React from 'react';
 const App = () => {
  const greeting = 'Hello Functional Component!';
 return <Headline value={greeting} />;
};
const Headline = ({ value }) => {
  return <h1>{value}</h1>;
};
export default App;

Class Components

A Class components are ES6 classes based Components. A component which provide more features to deal with codes. It also receives props like function components and is able to define its own states.
Each component in react has several life-cycle methods, which are being used from time to time.

Here are the list of Life-Cycle methods which are used depending on the requirements of code:

  1. constructor()
  2. getDerivedStateFromProps()
  3. render()
  4. componentDidMount()
  5. shouldComponentUpdate()
  6. getSnapshotBeforeUpdate()
  7. componentDidUpdate()
  8. shouldComponentUpdate()

We will go in depth of react life-cycle in our next article

Let’s take a example of class component

class Greeting extends React.Component {
constructor(props) {
    super(props);
  }
  render() {
    return <h1>Hello, Class Component</h1>;
  }
}
Export default Greeting

Pure Component

As per Reacts official documentation: React.PureComponent is similar to React.Component. The difference between them is that React.Component doesn’t implement shouldComponentUpdate(), but React.PureComponent implements it with a shallow prop and state comparison.

If your React component’s render() function renders the same result given the same props and state, you can use React.PureComponent for a performance boost in some cases.

Let's take an example while creating three files to understand the above official definition and how pure components works:

First file - index.js, import below created files here

import React, { Component } from 'react';
import Child from './Child';
import Pure from './pure';
 
class PureComp extends Component{
   constructor(props){
       super(props);
       this.state = {
           name: 'Shahab'
       }
   }
   shouldComponentUpdate(nextProps, nextState){
       if(nextState.name !== this.state.name){
           return true
       }
       return false
   }
   componentDidMount(){
       setInterval(() => {
           this.setState({
               name: 'Shahab'
           })
       }, 2000);
   }
 
   render(){
       return(
           <div>
               Parent Component: {this.state.name}   
               <Child />
               <Pure />
           </div>
       )
   }
}
export default PureComp;
 

Second file - pure.js

import React, { PureComponent } from 'react'
 class Pure extends PureComponent{
   render(){
       console.log('Pure Components')
       return(
           <div>Pure Components</div>
       )
   }
}
export default Pure

Third file - child.js

import React, {Component} from 'react'
 class Child extends Component{
   state ={
       name: 'Ibaad'
   }
   shouldComponentUpdate(nextProps, nextState){
       if(nextState.name !== this.state.name){
           return true
       }
       return false
   }
  
   render(){
       console.log('Child Components')
       return(
           <div>
               Child Component: {this.state.name}
           </div>
       )
   }
}
export default Child

Explanation of Shallow Comparison (SC) which are used in above pure components:
Primitive Types:
A (SC) b returns true if a and b have same values and are of same type.
Ex: string ‘Shahab’ (SC) string ‘Shahab’ returns true.

Complex Types
A (SC) b returns true if a and b reference the exact same object.
Ex in Array:

Var a = [1,2,3];
Var b = [1,2,3];
Var c = a;
Var ab_eq = (a === b) // false
Var ac_eq = (a === b) // true

Ex in Object:

Var a = {x = 1, x = 2};
Var b = {x = 1, x = 2};
Var c = a;
Var ab_eq = {a === c} // false
Var ac_eq = {a === b} // true

#reactjs #javascript #function #components #es6

Julie  Donnelly

Julie Donnelly

1602291900

How to Design Pure Functions

Today I am going to talk about how to design Pure Function and help you understand the benefits of pure function.

Nowadays Functional Programming is getting a lot of attention due to the advantages it offers like parallelismeasier testing_predictability _ and many others.

Pure functions is a concept mainly used in functional programming languages but it can be applied in any programming paradigm

Definition

Pure functions can be defined as

  • The function always returns the same value for the same inputs. The output is only determined by its input values.
  • A function where the return value is only determined by its input values, without observable side effects

Characteristics Of Pure Function

  • Function should always return a result.
  • Function must only depend on their inputs. That means this function should not use any other value than the function inputs to calculate the result
  • Pure functions cannot have other side effects than the computation of its return value.
  • Referentially transparent: An expression is said to be referentially transparent if
  • it can be replaced with its corresponding value without changing the
  • program’s behaviour.

#functional-programming #lambda-function #functor #kotlin #programming #functional-components #coding #coding-skills