<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zahidul Islam</title>
    <description>The latest articles on DEV Community by Zahidul Islam (@zahidulislam144).</description>
    <link>https://dev.to/zahidulislam144</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F781523%2F43e4e4f0-a9e3-4274-9df1-871ea85937bf.jpeg</url>
      <title>DEV Community: Zahidul Islam</title>
      <link>https://dev.to/zahidulislam144</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zahidulislam144"/>
    <language>en</language>
    <item>
      <title>Topic: JS Promise vs Async await</title>
      <dc:creator>Zahidul Islam</dc:creator>
      <pubDate>Thu, 30 Dec 2021 11:51:32 +0000</pubDate>
      <link>https://dev.to/zahidulislam144/topic-js-promise-vs-async-await-3dp1</link>
      <guid>https://dev.to/zahidulislam144/topic-js-promise-vs-async-await-3dp1</guid>
      <description>&lt;p&gt;As JavaScript is an Asynchronous(behaves synchronously), we need to use callbacks, promises and async await. You need to learn what is async-await, what is promise, how to use promise and async-await in javascript and where to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Promise&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What is Promise?&lt;/strong&gt;
A Promise is a rejected value or succeeded value of asynchronous function and when the operation is not fulfilled or fulfilled and thus promise is created. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Promise have three states.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;pending: initial state, neither fulfilled nor rejected.&lt;/li&gt;
&lt;li&gt;fulfilled: meaning that the operation was completed successfully.&lt;/li&gt;
&lt;li&gt;rejected: meaning that the operation failed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to use Promise? See the example below
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const calculate = (a,b,c) =&amp;gt; { 
  return new Promise((resolve, reject) =&amp;gt; {
  setTimeout(() =&amp;gt; {
    if(a&amp;lt;0 || b&amp;lt;0 || c||0){
       reject("no number can be negative.")
    }
    resolve('Calculation: ', a+b+c);
  }, 1000);
 }
}

calculate(5,7,8);
.then((reolveOfCalculate)=&amp;gt;{
  console.log(reolveOfCalculate);
}.catch((rejectOfCalculate)=&amp;gt;{
  console.log("rejectOfCalculate");
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From above example, I am trying to give a little explanation about promise. A promise is created in calculate function. When the operation of the function is fulfilled then, it calls a callBack function and the success value is kept in &lt;strong&gt;resolve&lt;/strong&gt; argument. Similarly, a callBack function is called, and the failure value is kept in &lt;strong&gt;reject&lt;/strong&gt; argument when the operation is not fulfilled. &lt;br&gt;
The success and rejected value is consoled by taking an argument respectively &lt;strong&gt;reolveOfCalculate&lt;/strong&gt;, &lt;strong&gt;rejectOfCalculate&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Promise can be written in chaining. See below...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const calculate = (a,b,c) =&amp;gt; { 
  return new Promise((resolve, reject) =&amp;gt; {
  setTimeout(() =&amp;gt; {
    if(a&amp;lt;0 || b&amp;lt;0 || c||0){
       reject("no number can be negative.")
    }
    resolve('Calculation: ', a+b+c);
  }, 1000);
 }
}

calculate(5,7,8);
.then((reolveOfCalculate)=&amp;gt;{
  console.log(reolveOfCalculate);
  return (reolveOfCalculate, 3, 2);
}.then((chaining1)=&amp;gt;{
  console.log(chaining1);
}).catch((rejectOfCalculate)=&amp;gt;{
  console.log("rejectOfCalculate");
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Async await&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Async await is the lighter version of promises. Because, basically promises works behind the await methods. The await operator is used to wait for a Promise. It can only be used inside an async function within regular JavaScript code. &lt;/p&gt;

&lt;h2&gt;
  
  
  Return value:
&lt;/h2&gt;

&lt;p&gt;Returns the fulfilled value of the promise, or the value itself if it's not a Promise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const calculate = (a,b,c) =&amp;gt; { 
  return new Promise((resolve, reject) =&amp;gt; {
  setTimeout(() =&amp;gt; {
    if(a&amp;lt;0 || b&amp;lt;0 || c||0){
       reject("no number can be negative.")
    }
    resolve('Calculation: ', a+b+c);
  }, 1000);
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = () =&amp;gt; {
   const sum1 = await calculate(1,2,3);
   const sum2 = await calculate(sum1,2,3);
   return sum2;
}
add().then((result)=&amp;gt;{console.log(result)}).catch((error)=&amp;gt;{console.log(error)};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why to use Async await:
&lt;/h2&gt;

&lt;p&gt;Async await is cleaner than promises. So, most of the programmers suggest to use async await to make our code readable and cleaner. &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Topic: JS Array Methods and their differences- forEach vs map vs filter vs find vs reduce vs slice vs splice</title>
      <dc:creator>Zahidul Islam</dc:creator>
      <pubDate>Wed, 29 Dec 2021 21:25:36 +0000</pubDate>
      <link>https://dev.to/zahidulislam144/topic-js-array-methods-and-their-differences-foreach-vs-map-vs-filter-vs-find-vs-reduce-3k6l</link>
      <guid>https://dev.to/zahidulislam144/topic-js-array-methods-and-their-differences-foreach-vs-map-vs-filter-vs-find-vs-reduce-3k6l</guid>
      <description>&lt;p&gt;We all know about JavaScript array methods and their use cases also. But sometimes, there will create a bit of confusion among map, forEach, filter, find and reduce. Because, all of them work in array element and gives similar output also. But a question is where are the differences ? Now I am going to write my a short research about them and their use cases. &lt;/p&gt;

&lt;p&gt;Before writing my explanation, I need to clarify a thing shortly that what the higher order function in JavaScript is. Because, in which topic I am going to write, all of them are a higher order function. Basically, The function will be a higher function when it takes a function as a parameter. The parameter function can be anonymous function or arrow function. &lt;/p&gt;

&lt;h2&gt;
  
  
  - &lt;strong&gt;forEach()&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;forEach() is nothing but a traditional for loop. It works like a for loop. It gives an array looping over each array element of our existing array and perform operation on them. One thing is point to be noted, that forEach doesn't return an array. It returns undefined.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Arrow function
forEach((element) =&amp;gt; { /* ... */ } )
forEach((element, index) =&amp;gt; { /* ... */ } )
forEach((element, index, array) =&amp;gt; { /* ... */ } )

// Callback function
forEach(callbackFn)
forEach(callbackFn, thisArg)

// Inline callback function
forEach(function(element) { /* ... */ })
forEach(function(element, index) { /* ... */ })
forEach(function(element, index, array){ /* ... */ })
forEach(function(element, index, array) { /* ... */ }, thisArg)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = ['a', 'b', 'c'];

array1.forEach(element =&amp;gt; console.log(element));

// expected output: "a"
// expected output: "b"
// expected output: "c"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  - &lt;strong&gt;map()&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;map() works on array object like forEach() method. But there is a little difference is that map creates a new array like the given array transforming and modifying existing array element. It returns array that is created. &lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Arrow function
array.map((element) =&amp;gt; { /* ... */ })
array.map((element, index) =&amp;gt; { /* ... */ })
array.map((element, index, array) =&amp;gt; { /* ... */ })

// Callback function
array.map(callbackFn)
array.map(callbackFn, thisArg)

// Inline callback function
array.map(function(element) { /* ... */ })
array.map(function(element, index) { /* ... */ })
array.map(function(element, index, array){ /* ... */ })
array.map(function(element, index, array) { /* ... */ }, thisArg)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x =&amp;gt; x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  - &lt;strong&gt;filter()&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;filter() works on array object or element, but the difference is that it only works on condition based. In a simple word, filter returns creating a subset of array according to your condition given in a callback function. &lt;br&gt;
For example, When you need to some elements or objects that are not needed and wanted to remove them, then need to use filter method. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"&gt;see more&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Syntax:
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Arrow function
filter((element) =&amp;gt; { /* ... */ } )
filter((element, index) =&amp;gt; { /* ... */ } )
filter((element, index, array) =&amp;gt; { /* ... */ } )

// Callback function
filter(callbackFn)
filter(callbackFn, thisArg)

// Inline callback function
filter(function(element) { /* ... */ })
filter(function(element, index) { /* ... */ })
filter(function(element, index, array){ /* ... */ })
filter(function(element, index, array) { /* ... */ }, thisArg)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;elem  :element value&lt;br&gt;
index :index in each traversal, moving from left to right&lt;br&gt;
array :original array invoking the method&lt;br&gt;
thisArg   :(Optional) object that will be referred to as this in callback&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word =&amp;gt; word.length &amp;gt; 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  - &lt;strong&gt;find()&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;find() method also works on array element but the difference is, it returns the value or existing array element when the condition of the given callback function is true otherwise it will return undefined.  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find"&gt;see more&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Arrow function
find((element) =&amp;gt; { /* ... */ } )
find((element, index) =&amp;gt; { /* ... */ } )
find((element, index, array) =&amp;gt; { /* ... */ } )

// Callback function
find(callbackFn)
find(callbackFn, thisArg)

// Inline callback function
find(function(element) { /* ... */ })
find(function(element, index) { /* ... */ })
find(function(element, index, array){ /* ... */ })
find(function(element, index, array) { /* ... */ }, thisArg)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element =&amp;gt; element &amp;gt; 10);

console.log(found);
// expected output: 12

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  - &lt;strong&gt;reduce()&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;reduce() method is used to reduce the array into a single value. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce"&gt;see more&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/ Arrow function
reduce((previousValue, currentValue) =&amp;gt; { /* ... */ } )
reduce((previousValue, currentValue, currentIndex) =&amp;gt; { /* ... */ } )
reduce((previousValue, currentValue, currentIndex, array) =&amp;gt; { /* ... */ } )
reduce((previousValue, currentValue, currentIndex, array) =&amp;gt; { /* ... */ }, initialValue)

// Callback function
reduce(callbackFn)
reduce(callbackFn, initialValue)

// Inline callback function
reduce(function(previousValue, currentValue) { /* ... */ })
reduce(function(previousValue, currentValue, currentIndex) { /* ... */ })
reduce(function(previousValue, currentValue, currentIndex, array) { /* ... */ })
reduce(function(previousValue, currentValue, currentIndex, array) { /* ... */ }, initialValue)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3, 4];
const reducer = (previousValue, currentValue) =&amp;gt; previousValue + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  - &lt;strong&gt;slice()&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;slice() method is used to copy a specific portion of an array. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice"&gt;see more&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;slice()&lt;br&gt;
slice(start)&lt;br&gt;
slice(start, end)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]

console.log(animals.slice(-2));
// expected output: Array ["duck", "elephant"]

console.log(animals.slice(2, -1));
// expected output: Array ["camel", "duck"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  - &lt;strong&gt;splice()&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;splice() method is used when we need to delete or replace a specific index of an array. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice"&gt;see more&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;splice(start)&lt;br&gt;
splice(start, deleteCount)&lt;br&gt;
splice(start, deleteCount, item1)&lt;br&gt;
splice(start, deleteCount, item1, item2, itemN)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Topic: Node Emit</title>
      <dc:creator>Zahidul Islam</dc:creator>
      <pubDate>Tue, 28 Dec 2021 20:41:16 +0000</pubDate>
      <link>https://dev.to/zahidulislam144/topic-node-emit-35m4</link>
      <guid>https://dev.to/zahidulislam144/topic-node-emit-35m4</guid>
      <description>&lt;p&gt;Node Js has built in event module to handle event. I expect that we all know what the event is. Actually, event create an action when user triggered. Events can be user-generated, such as keystrokes and mouse clicks. But these events are built in. We can't make our event as our choice. In node js, we can make our event. &lt;a href="https://nodejs.org/api/events.html#class-eventemitter"&gt;Node Event&lt;/a&gt; by clicking this you can get the official docs. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Node Event&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;One of the core concepts of Node.js is events. Event Emitter plays a vital role in Node.js Event-Driven Architecture.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;How does it work?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Node Emit works in three ways. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Event Emitter emits the data in an event called message&lt;/li&gt;
&lt;li&gt;A Listened is registered on the event message&lt;/li&gt;
&lt;li&gt;when the message event emits some data, the listener will get the data.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;EventEmitter methods and description&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;on(): on(event, listener), by this method we can add event listener. It is just like 'addListener' keyword in JavaScript. &lt;/li&gt;
&lt;li&gt;emit(): emit(event, arg1, arg2), by this event we call the specified event listener. It is just like 'onclick' keyword in JavaScript.&lt;/li&gt;
&lt;li&gt;once(): once(event, listener), by this method the event is called for one time and after that it will be removed. &lt;/li&gt;
&lt;li&gt;addListener(): addListener(event, listener), by this method the event is checked if it is registered or not.&lt;/li&gt;
&lt;li&gt;removeListener(): removeListener(event, listener), by this method the event is removed for listener.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Example:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const EventEmitter = require("events")
const emitter = new EventEmitter()
emitter.on("message", (arg) =&amp;gt; {
  console.log("subsribed", message)
})
emitter.emit("message", "Welcome to my blog.")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  From the above example, we can say that EventEmitter is a built-in class of event in Node JS. Normally, we declare our variable name with lower case at start, but here we should start with upper case letter in declaring event emitter. When we import 'events' class where methods are written. After creating an object, we can access these methods. So, 'emitter' is a object of that particular class, and it accesses the on() method and emit() method over there.
&lt;/h6&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Use Case&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;At first, create a file named "timer.js". The code will be the following example given below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const EventEmitter = require("events")
class Timer extends EventEmitter {
  closeIt('close', msg) {
    this.emit('close', msg)
  }
}
module.exports = Timer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Here, we can see, Timer extends the EventEmitter class and the method called closeIt takes event and message as an argument.
&lt;/h6&gt;

&lt;p&gt;Now create 'app.js' file and write this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Timer = require("./Timer")
const timer = new Timer()
timer.on("close", arg =&amp;gt; {
  console.log("Listened value: ", arg)
})
timer.closeIt("close: ", 'Time out ! Server will be closing soon...')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Here, it requires the module Timer and instantiates the class as a timer. closeIt() function emits event called 'close'. The listener will listen for the event and close it.
&lt;/h6&gt;

</description>
    </item>
    <item>
      <title>Topic: React Hook and Custom React Hook</title>
      <dc:creator>Zahidul Islam</dc:creator>
      <pubDate>Tue, 28 Dec 2021 10:32:10 +0000</pubDate>
      <link>https://dev.to/zahidulislam144/topic-react-hook-and-custom-react-hook-5d6l</link>
      <guid>https://dev.to/zahidulislam144/topic-react-hook-and-custom-react-hook-5d6l</guid>
      <description>&lt;h2&gt;
  
  
  When I was learning to React, some parts seemed to me difficult. Today, I have decided to write some of my learnings and ideas about react hook and custom react hook.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  - &lt;strong&gt;What is hook, basically?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In real life, hook means a one kind of ring that holds something. In react, hook works basically based on this following concept such as if we use React hook, there will be a state that stores data and provides data in any component during setting the states in a component or multiple component using the same state. As we can use the same state in many components, so it is thought that it holds data like a ring and can be linked in any component. But there have some rules and guidelines to use react hooks. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Hook must be declared at the top of the return functions and inside the React function.&lt;/li&gt;
&lt;li&gt;Don’t declare hooks in loop, conditions and nested functions. &lt;/li&gt;
&lt;li&gt;Hook can be written in functional component only.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// imported hooks to use from react
import React, { useEffect, useState } from "react";

//react function
const MyOrder = () =&amp;gt; {

// setting react hook
const [order, setMyOrder] = useState(‘’ ”) / useState(null);

    retrun(
       // code contents in JSX format.
    );
}
export default MyOrder;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;From, above example, we can say that, this is a React functional component. Now I am going to give explanation. &lt;/li&gt;
&lt;li&gt;Firstly, we create a ‘MyOrder.js’ file as a component that handles order related task. The component name must be started with an uppercase letter. &lt;/li&gt;
&lt;li&gt;Secondly, export the created component for further use in other component. If it isn’t exported, it can’t be imported in another component. &lt;/li&gt;
&lt;li&gt;Thirdly, we need to define our hook. Hook always start ‘use’ key word. In the above example, there is used useState hook. Now, come to the main point that how to set our state and how to store data in state? In state, data is stored in array format. So, at first we take two elements inside an array [order, setOrder]. Here, setOrder sets our data into an order element. When, setOrder element is first called, it renders the React component and store the data in a memory cell in order element. When it is rendered again, it will create another memory call to store the data. But when it is rendering again and again, it returns the previous stored data serially except creating a new cell. So, we should set hook serially to reduce creating bugs.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  - &lt;strong&gt;What is useEffect hook, basically?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;useEffect is also a hook and one kind of function. It is a call back function. Because Each time it passes a call back function after rendering a component. It can pass an array with call back function. This call back function will be called with the first render of the component and after that it will be called with the changing of the array. But if there is no array element, then it will be called once for the first time of rendering of the component. One thing is to be noted that, the array element is called dependency of useEffect. &lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    fetch(`http://localhost:8000/premium-autos/select-now/${id}`)
      .then((res) =&amp;gt; res.json())
      .then((data) =&amp;gt; {
        console.log(data);
        setOrder(data);
      });
  }, [id]);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, after the render of the component, the useEffect hook is called and fetch the URL and send a response. Basically, this hook is used to fetch data from API. When, we need to set some dependency that data will be fetched with a specific id or email, then we can set a dependency in array. Here, [id] is the dependency. &lt;/p&gt;

&lt;h2&gt;
  
  
  - &lt;strong&gt;Benefits of Hooks&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;We can write many function in hooks, and use in other components easily.&lt;/li&gt;
&lt;li&gt;It makes our component easy for reusability, readability and testing. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  - &lt;strong&gt;What is custom hook, basically?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Custom hook is nothing but a function. When we need to write a specific logic for any project and need to use same logic in other components, then custom hook plays an important role. Just we need to create a JS file named ‘useAuth.js’ as an example, here we can write any name also but writing ‘use’ at first it looks quite good. It’s just a convention of naming a custom hook. In custom hook, there will be written necessary function. Here, I have given an example ‘useAuth.js’ Because authentication is needed in every component&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from "react";

//react function and it will be our custom hook
const useAuth = ( ) =&amp;gt; {

// setting react hook
const [userName, setUserName] = useState(‘’ ”) / useState(null);

const handleInputField = ( ) =&amp;gt; {
    // code contents
}

const handleForm = ( ) =&amp;gt; {
    // code contents
}

// returning function so that these function can be accessed in other components when custom hook will be called
    retrun(
          handleInputField,
          handleForm
    );
}
export default useAuth;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
    </item>
  </channel>
</rss>
