<?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: Muhammad Bilal Bangash</title>
    <description>The latest articles on DEV Community by Muhammad Bilal Bangash (@bangash1996).</description>
    <link>https://dev.to/bangash1996</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F285180%2Fdcf91e58-5614-4782-9ea6-04951de63d83.png</url>
      <title>DEV Community: Muhammad Bilal Bangash</title>
      <link>https://dev.to/bangash1996</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bangash1996"/>
    <language>en</language>
    <item>
      <title>Detecting user leaving page with react-router-dom v6.0.2</title>
      <dc:creator>Muhammad Bilal Bangash</dc:creator>
      <pubDate>Sun, 23 Jan 2022 13:25:25 +0000</pubDate>
      <link>https://dev.to/bangash1996/detecting-user-leaving-page-with-react-router-dom-v602-33ni</link>
      <guid>https://dev.to/bangash1996/detecting-user-leaving-page-with-react-router-dom-v602-33ni</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Before reading this article you should know about React Routing, its working.&lt;/p&gt;

&lt;p&gt;Main Focus of this post is detecting user leaving page with react-router-dom v6.0.2.&lt;/p&gt;

&lt;p&gt;You can use &lt;strong&gt;usePrompt&lt;/strong&gt; or &lt;strong&gt;useBlocker&lt;/strong&gt; to detect and show a prompt before leaving to another route if they have any unsaved changes.&lt;/p&gt;

&lt;p&gt;However, in the &lt;a href="https://reactrouter.com/docs/en/v6/upgrading/v5" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; of react router v6 the following is mentioned:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;from v5 (along with usePrompt and useBlocker from the v6 betas) are not included in the current released version of v6.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But there are two different solution to achieve your goal.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Either you can downgrade to v5 or 6.0.0-alpha.5 to use &lt;strong&gt;usePrompt&lt;/strong&gt; &amp;amp; &lt;strong&gt;useBlocker&lt;/strong&gt; in you project/code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Second solution is create &lt;strong&gt;custom hook&lt;/strong&gt; instead of downgrading.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Post Focus useCallbackPrompt &amp;amp; useBlocker custom hooks
&lt;/h2&gt;

&lt;p&gt;In my today's post I will focus on my second solution how did I created a custom hook to resolve my problem.&lt;/p&gt;

&lt;p&gt;for this I created a small project for demo.&lt;br&gt;
Here is the project link &lt;a href="https://github.com/Bilal-Bangash/detecting-route-change-react-route-dom-v6" rel="noopener noreferrer"&gt;GITHUB&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this project I created two routes for Home and About page and in Home Page there is simple form with Name and Designation Field.&lt;br&gt;
My goal was when user type something on form and trying to leave this page or route prompt/DialogBox will be shown that there are some changes.....&lt;/p&gt;

&lt;p&gt;how my folder structure looks like&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fponb5adwnjok7kyku19a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fponb5adwnjok7kyku19a.png" alt="useCallbackPrompt Hook"&gt;&lt;/a&gt;&lt;br&gt;
in above screenshot I highlighted the custom hooks that I created in this project&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;useBlocker&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;useCallbackPrompt&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  useCallbackPrompt Hook
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcdnzytjxt9t8zpztyqen.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcdnzytjxt9t8zpztyqen.png" alt="useCallbackPrompt Hook"&gt;&lt;/a&gt;&lt;br&gt;
this hooks returns three things 2 boolean variables and 1 function. Basically for handling DialogBox to show or hide &lt;br&gt;
here is the exact file for that &lt;a href="https://github.com/Bilal-Bangash/detecting-route-change-react-route-dom-v6/blob/master/src/hooks/useCallbackPrompt.ts" rel="noopener noreferrer"&gt;useCallbackPrompt Hook&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  useBlocker Hook
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6pfm3qbgixv842iuqe2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6pfm3qbgixv842iuqe2.png" alt="useBlocker Hook"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;this hook basically blocks user from navigating away if there are any changes&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Bilal-Bangash/detecting-route-change-react-route-dom-v6/blob/master/src/hooks/useBlocker.ts" rel="noopener noreferrer"&gt;useBlocker Hook&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Now Question arise How I am using this in my project
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp56b0n7l23545xgnzusi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp56b0n7l23545xgnzusi.png" alt="useBlocker Hook Code"&gt;&lt;/a&gt;&lt;br&gt;
What I am doing here when user type something handleChange will trigger and update the showDialog to &lt;strong&gt;true&lt;/strong&gt; and I am passing &lt;strong&gt;showDialog&lt;/strong&gt; to &lt;strong&gt;useCallbackPrompt&lt;/strong&gt; and when user trying to navigate away a prompt will be shown &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyg6ts3ffswwkv324dj6x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyg6ts3ffswwkv324dj6x.png" alt="Home page Code Screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Bilal-Bangash/detecting-route-change-react-route-dom-v6/blob/master/src/pages/Home.page.tsx" rel="noopener noreferrer"&gt;Home Page&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is &lt;a href="https://detect-user-leaving-page-react-router-dom-v6.netlify.app/home" rel="noopener noreferrer"&gt;Live Demo Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is &lt;a href="https://github.com/Bilal-Bangash/detecting-route-change-react-route-dom-v6" rel="noopener noreferrer"&gt;Git Repo Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Further improvement, suggestion or help. Welcome :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>routing</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Set in JavaScript</title>
      <dc:creator>Muhammad Bilal Bangash</dc:creator>
      <pubDate>Sat, 12 Jun 2021 12:14:42 +0000</pubDate>
      <link>https://dev.to/bangash1996/set-in-javascript-36fb</link>
      <guid>https://dev.to/bangash1996/set-in-javascript-36fb</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"Inner Experience is only one source of human Knowledge." &lt;br&gt;
                              - Dr. Allama Muhammad Iqbal&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Good Day!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, we would be discussing the &lt;strong&gt;&lt;code&gt;Set&lt;/code&gt;&lt;/strong&gt; object in &lt;strong&gt;JavaScript&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Set&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instance properties&lt;/strong&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;size&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instance methods&lt;/strong&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;add(value)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;delete(value)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;clear()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;has(value)&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iteration methods&lt;/strong&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;keys()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;values()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;entries()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;forEach()&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Remove duplicate items from array using Set&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's Start.....&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Set
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Set&lt;/code&gt; object lets you store unique values of any type, whether primitive values or object references.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Set&lt;/code&gt; objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the &lt;code&gt;Set&lt;/code&gt; may only occur once; it is unique in the &lt;code&gt;Set's&lt;/code&gt; collection.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setObj= new Set();
setObj.add("anyString");
setObj.add(11);
setObj.add(true);
setObj.add({key:"value"});

console.log("Data in Set is ",setObj)
// Data in Set is Set(4) {"anyString", 11, true, {key:"value"}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Instance properties
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;size:&lt;/strong&gt; Returns the number of values in &lt;code&gt;Set&lt;/code&gt; object
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setObj= new Set();
setObj.add("anyString");
setObj.add(11);
setObj.add(true);
setObj.add({key:"value"});

console.log("Size of Set is ",setObj.size)
// Size of Set is 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Instance methods
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;add(value):&lt;/code&gt;&lt;/strong&gt; Appends &lt;code&gt;value&lt;/code&gt; to the &lt;code&gt;Set&lt;/code&gt; object. Return the &lt;code&gt;Set&lt;/code&gt; object with added value
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setObj= new Set();
setObj.add("addedString");
console.log("First value added String ", setObj);
// First Value added String Set(1) {"addedString"}
setObj.add(11);
console.log("second value added number", setObj);
// second value added number Set(2) {"addedString", 11}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;delete(value):&lt;/code&gt;&lt;/strong&gt; Removes the element associated to the value and returns a &lt;code&gt;boolean&lt;/code&gt; asserting whether an element was successfully removed or not.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setObj= new Set();
setObj.add("addedString");
console.log("Delete addedString from setObj ", setObj.delete("addedString"));
// Delete addedString from setObj true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;clear():&lt;/code&gt;&lt;/strong&gt; Removes all elements from the &lt;code&gt;Set&lt;/code&gt; object.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setObj= new Set();
setObj.add("anyString");
setObj.add(11);
setObj.add(true);
setObj.add({key:"value"});

console.log("Data in Set is ",setObj)
// Data in Set is Set(4) {"anyString", 11, true, {key:"value"}}


//Now clear setObj
setObj.clear();

console.log("Data in Set after clear ",setObj)
// Data in Set after clear Set(0) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;has(value):&lt;/code&gt;&lt;/strong&gt; Returns a &lt;code&gt;boolean&lt;/code&gt; asserting whether an element is present with the given value in the &lt;code&gt;Set&lt;/code&gt; object or 
not.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setObj= new Set();
setObj.add("anyString");
setObj.add(11);
setObj.add(true);
setObj.add({key:"value"});

console.log("Data in Set is ",setObj)
// Data in Set is Set(4) {"anyString", 11, true, {key:"value"}}


//Now check value exist or not in setObj

console.log("11 exists in setObj ",setObj.has(11));
// 11 exists in setObj true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Iteration methods
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;keys() &amp;amp;&amp;amp; values():&lt;/code&gt;&lt;/strong&gt; Returns a new iterator object that yields the values for each element in the &lt;code&gt;Set&lt;/code&gt; object in insertion order. (For Sets, &lt;code&gt;keys()&lt;/code&gt; &amp;amp;&amp;amp; &lt;code&gt;values()&lt;/code&gt; methods are same.)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setObj= new Set();
setObj.add("anyString");
setObj.add(11);
setObj.add(true);
setObj.add({key:"value"});

for (let item of setObj.keys()) console.log('Item: ',item)
// Item: anyString
// Item: 11
// Item: true
// Item: {key:"value"}

for (let item of setObj.values()) console.log('Item: ',item)
// Item: anyString
// Item: 11
// Item: true
// Item: {key:"value"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;forEach(callbackFn[, thisArg]):&lt;/code&gt;&lt;/strong&gt; Calls &lt;code&gt;callbackFn&lt;/code&gt; once for each value present in the &lt;code&gt;Set&lt;/code&gt; object, in insertion order. If a &lt;code&gt;thisArg&lt;/code&gt; parameter is provided, it will be used as the &lt;code&gt;this&lt;/code&gt; value for each invocation of &lt;code&gt;callbackFn&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setObj= new Set();
setObj.add("anyString");
setObj.add(11);
setObj.add(true);
setObj.add({key:"value"});

setObj.forEach((value)=&amp;gt;{
  console.log('Value: ',value)
})

// Value: anyString
// Value: 11
// Value: true
// Value: {key:"value"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Remove duplicate items from array using Set
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const duplicateNumbers = [11,45,33,11,22,44,44,44,56,76,45];
console.log('duplicate removed ',[...new Set(duplicateNumbers)]);

// duplicate removed (7) [11,45,33,22,44,56,76]

const dupNames=["John","Daina","John","Jason","Joe","Daina"];
console.log('duplicate removed ',[...new Set(dupNames)]);

// duplicate removed  (4) ["John", "Daina", "Jason", "Joe"]

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

&lt;/div&gt;



&lt;p&gt;Here it's official documentation &lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"&gt;Set in JavaScript Official&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.&lt;br&gt;
Thank you&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
      <category>ecmascript</category>
    </item>
    <item>
      <title>Introduction React-Redux using Hooks (useSelector &amp;&amp; useDispatch)</title>
      <dc:creator>Muhammad Bilal Bangash</dc:creator>
      <pubDate>Sat, 05 Jun 2021 17:59:21 +0000</pubDate>
      <link>https://dev.to/bangash1996/introduction-react-redux-using-hooks-useselector-usedispatch-26ch</link>
      <guid>https://dev.to/bangash1996/introduction-react-redux-using-hooks-useselector-usedispatch-26ch</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Before reading this article you should know about React &amp;amp; Redux, its working.&lt;/p&gt;

&lt;p&gt;This article is about React-Redux Hooks. We will go through below main points in this article:&lt;/p&gt;

&lt;h4&gt;
  
  
  * Hooks for Redux.
&lt;/h4&gt;

&lt;h4&gt;
  
  
  * How to use &lt;code&gt;useDispatch&lt;/code&gt; Hook.
&lt;/h4&gt;

&lt;h4&gt;
  
  
  * How to use &lt;code&gt;useSelector&lt;/code&gt; Hook.
&lt;/h4&gt;

&lt;h3&gt;
  
  
  1. Hooks for Redux
&lt;/h3&gt;

&lt;p&gt;Before Hooks, we always used a &lt;code&gt;connect()&lt;/code&gt; which is a higher-order component and wrapper to our component, &lt;code&gt;connect()&lt;/code&gt; read values from the Redux store.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;connect()&lt;/code&gt; takes two arguments, both optional:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;mapStateToProps&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mapDispatchToProps&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;mapStateToProps:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;called every time the store state changes. It receives the entire store state and should return an object of data this component needs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;mapDispatchToProps:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This parameter can either be a function, or an object. If it’s a function, it will be called once on component creation. It will receive dispatch as an argument and should return an object full of functions that use dispatch to dispatch actions.&lt;/p&gt;

&lt;p&gt;more about &lt;a href="https://react-redux.js.org/api/connect"&gt;connect()&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's move towards react-redux hooks. React-Redux now offers a set of hook APIs as an alternative to existing &lt;code&gt;connect()&lt;/code&gt; Higher-Order Component. These APIs allow you to subscribe to the Redux store and dispatch actions, without having to wrap your components in &lt;code&gt;connect()&lt;/code&gt;. By using the Hook API with Function components, components are kept small and the code remains clean.&lt;/p&gt;

&lt;h4&gt;
  
  
  Hooks:
&lt;/h4&gt;

&lt;h3&gt;
  
  
  2. useDispatch():
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;useDispatch()&lt;/code&gt; hook is equivalent of &lt;code&gt;mapDispatchToProps&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will invoke &lt;code&gt;useDispatch&lt;/code&gt; and store it to a variable, &lt;code&gt;dispatch&lt;/code&gt;. This hook returns a &lt;code&gt;reference&lt;/code&gt; to the &lt;code&gt;dispatch function&lt;/code&gt; from the Redux store. You may use it to dispatch actions as needed.&lt;br&gt;
And we dispatch it by calling dispatch passing in the return value from the action creator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to use&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below is the small component where using &lt;code&gt;useDispatch&lt;/code&gt; and &lt;code&gt;useSelector&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;import React from "react";
//import useDispatch from react-redux
import { useDispatch} from "react-redux";
//these are actions define in redux&amp;gt;actions folder
import { updateFirstName } from "../redux/actions"; 

const Form = () =&amp;gt; {

  const dispatch = useDispatch();

  const handleFirstName = () =&amp;gt; {
    //dispatching the action
    dispatch(updateFirstName("Jason"));
  };

  return (
    &amp;lt;React.Fragment&amp;gt;
      &amp;lt;div className="container"&amp;gt;
        &amp;lt;button onClick={handleFirstName}&amp;gt;Update First 
        Name&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/React.Fragment&amp;gt;
  );
};

export default Form;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;complete code in &lt;a href="https://github.com/Bilal-Bangash/redux-hooks"&gt;GITHUB redux-hooks&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3.useSelector():
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;useSelector()&lt;/code&gt; hook is equivalent of &lt;code&gt;mapStateToProps&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useSelector&lt;/code&gt; is a function that takes the current state as an argument and returns whatever data you want from it and it allows you to store the return values inside a variable within the scope of you functional components instead of passing down as props&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 from "react";
import { useDispatch, useSelector } from "react-redux";
import { updateFirstName } from "../redux/actions";

const Form = () =&amp;gt; {
  const dispatch = useDispatch();
  const nameObj = useSelector((state) =&amp;gt; state.nameReducer);
  const { firstName } = nameObj;
  const handleFirstName = () =&amp;gt; {
    dispatch(updateFirstName("Jason"));
  };

  return (
    &amp;lt;React.Fragment&amp;gt;
      &amp;lt;div className="container"&amp;gt;
        &amp;lt;label&amp;gt;First Name : {firstName}&amp;lt;/label&amp;gt;
        &amp;lt;button onClick={handleFirstName}&amp;gt;Update First Name&amp;lt;/button&amp;gt;

        &amp;lt;label&amp;gt;Last Name : {lastName}&amp;lt;/label&amp;gt;
        &amp;lt;button type="submit" onClick={handleLastName}&amp;gt;
          Update First Name
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/React.Fragment&amp;gt;
  );
};

export default Form;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;complete code in &lt;a href="https://github.com/Bilal-Bangash/redux-hooks"&gt;GITHUB redux-hooks&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  useStore():
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;useStore()&lt;/code&gt; hook returns a reference to the same Redux store that was passed into &lt;code&gt;Provider&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;This hook should probably not be used frequently. Prefer useSelector() as your primary choice. However, this may be useful for less common scenarios that do require access to the store, such as replacing reducers.&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 from 'react'
import { useStore } from 'react-redux'

export const ExampleComponent = ({ value }) =&amp;gt; {
  const store = useStore()

  // EXAMPLE ONLY! Do not do this in a real app.
  // The component will not automatically update if the store state changes
  return &amp;lt;div&amp;gt;{store.getState().obj.name}&amp;lt;/div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;complete code in &lt;a href="https://github.com/Bilal-Bangash/redux-hooks"&gt;GITHUB redux-hooks&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you wanna learn more about &lt;code&gt;useDispatch&lt;/code&gt; and &lt;code&gt;useSelector&lt;/code&gt; here it's official link &lt;a href="//react-redux.js.org/api/hooks"&gt;React Redux Hooks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Further improvement, suggestion or help. Welcome :) &lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript new features in ECMAScript 2021</title>
      <dc:creator>Muhammad Bilal Bangash</dc:creator>
      <pubDate>Tue, 01 Jun 2021 07:23:57 +0000</pubDate>
      <link>https://dev.to/bangash1996/javascript-new-features-in-ecmascript-2021-1gg8</link>
      <guid>https://dev.to/bangash1996/javascript-new-features-in-ecmascript-2021-1gg8</guid>
      <description>&lt;p&gt;Below are some new features that you can expect from the ECMAScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;replaceAll&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Promise.any&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;WeakRefs and finalizer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Logical Assignment Operators&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Numeric separator&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's begin with the first one.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. replaceAll() method
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;replaceAll()&lt;/strong&gt; method allows you to replace all occurrences of a substring with another string that you defined. Currently, the &lt;strong&gt;replace()&lt;/strong&gt; method only replace the first occurrence of the substring while ignoring the rest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = 'How+are+you?';
const addSpaces = str.replace('+', ' ');
console.log(addSpaces); // How are+you?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only way to replace all occurrences is to use &lt;strong&gt;replace()&lt;/strong&gt; with a global regular expression as follows:&lt;br&gt;
&lt;code&gt;// replace all '+' with a 'space'&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 str = 'How+are+you?';
const addSpaces = str.replace(/\+/g, ' ');
console.log(addSpaces); // How are you?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the &lt;strong&gt;replaceAll()&lt;/strong&gt; method, you don’t have to use a regular expression anymore:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = 'How+are+you?';
const addSpaces = str.replaceAll('+', ' ');
console.log(addSpaces) //How are you?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Promise.any()
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Promise.any()&lt;/strong&gt; method returns a promise that will resolve as soon as one of the promises are resolved. If all of the promises are rejected, the method will throw an AggregateError exception holding the rejection reasons.&lt;br&gt;
Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const firstPromise = new Promise((resolve, reject) =&amp;gt; {
  setTimeout(() =&amp;gt; resolve("First Promise Fulfilled"), 3000);
});
const secondPromise = new Promise((resolve, reject) =&amp;gt; {
  setTimeout(() =&amp;gt; resolve("Second Promise Fulfilled"), 2000);
});
const thirdPromise = new Promise((resolve, reject) =&amp;gt; {
  setTimeout(() =&amp;gt; resolve("Third Promise Fulfilled"), 1000);
});
Promise.any([firstPromise, secondPromise, thirdPromise]).then(
  (promiseFulfilled) =&amp;gt; {
    // Any of the promises was fulfilled.
    console.log(promiseFulfilled); // Third Promise Fulfilled
  },
  (error) =&amp;gt; {
    console.log(error)// Error Handling
  }
);

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

&lt;/div&gt;



&lt;p&gt;If all of the promises given are rejected, the AggregateError exception will be thrown.&lt;br&gt;
Here’s another example showing the exception using the async/await syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const firstPromise = new Promise((resolve, reject) =&amp;gt; {
  setTimeout(() =&amp;gt; reject(), 1000);
});
try {
  const first = await Promise.any([firstPromise]);
  // Any of the promises was fulfilled.
} catch (error) {
  console.log(error);
  // AggregateError: All promises were rejected
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. WeakRefs
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;WeakRef&lt;/strong&gt;, which stands for &lt;strong&gt;Weak References&lt;/strong&gt;, allows you to create a weak reference to an object. The primary use of Weak Reference is to implement caches or mappings of a large object.&lt;br&gt;
A regular/ strong JavaScript object reference will never be claimed by the garbage collector. A weak reference to an object, in contrast, can be claimed by JavaScript garbage collector:&lt;br&gt;
&lt;code&gt;const simpleObj = {name: "John Doe"};&lt;br&gt;
const referenceObj = new WeakRef({name: "John Doe"});&lt;/code&gt;&lt;br&gt;
When you need to read the value of WeakRefs , you need to use the &lt;strong&gt;deref()&lt;/strong&gt; method to return the instance’s target object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const referenceObj = new WeakRef({name: "John Doe"});
const obj = referenceObj.deref();
console.log(obj.name); // John Doe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the implementation detail of when, how, and whether JavaScript garbage collection actually occurs or not is up to the JavaScript engine implementation, you may observe different behavior between one JavaScript environment and another.&lt;br&gt;
The correct use of &lt;strong&gt;WeakRef&lt;/strong&gt; takes careful thought, and it’s best to avoid implementing one if possible. Unless you’re a JavaScript library maintainer, you will most likely never need to use it.&lt;/p&gt;
&lt;h3&gt;
  
  
  4. Finalizers
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Finalizer&lt;/strong&gt; is a companion feature of WeakRef that allows you to execute a piece of code after an object has become unreachable to the program.&lt;br&gt;
In short, you can register a callback function that gets triggered after the garbage collection occurs. You can create a registry by passing the callback to the &lt;strong&gt;FinalizationRegistry&lt;/strong&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const registry = new FinalizationRegistry(value =&amp;gt; {
  console.log(value);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, you can register any object you want to cleanup for by calling the register() method, passing both the object and the value you want to pass to the callback function:&lt;br&gt;
&lt;code&gt;registry.register({name: "John"}, "any value");&lt;/code&gt;&lt;br&gt;
The object passed into the register() method will be weak-referenced so that it can be garbage collected. Based on the code above, the callback will log “any value” to the console.&lt;br&gt;
Both WeakRefs and Finalizers are tricky concepts. You can learn more about weak reference and FinalizationRegistry. &lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef"&gt;WeakRef&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry"&gt;FinalizationRegistry&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  5. Logical assignment operator
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; ||= &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR_assignment"&gt;Logical OR assignment&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; &amp;amp;&amp;amp;= &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND_assignment"&gt;Logical AND assignment &lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; ??= &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_nullish_assignment"&gt;Logical nullish assignment&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The logical assignment operator combines Logical Operators and Assignment Expressions, allowing you to write a shorter syntax for variable value checking.&lt;/p&gt;

&lt;p&gt;For example, the following code checks whether the value of x is falsy and only assign a new value when it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x;
if(!x){
  x = 7;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the logical assignment operator, you can replace the above code with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x;
**x ||= 11;** // since x is undefined, it's assigned the number 11
console.log(x); // 11
The logical assignment works with logical AND (**&amp;amp;&amp;amp;**) and nullish coalescing operator (**??**) as well:
let x = null;
x ??= 11 // assign 11 to x when it's null or undefined
let y = "JavaScript";
y &amp;amp;&amp;amp;= 11 // assign 11 to y when it's value is truthy

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Numeric separator
&lt;/h3&gt;

&lt;p&gt;The numeric separator proposal helps you to write more readable code by allowing you to use underscore (&lt;strong&gt;_&lt;/strong&gt;) as a separator when you define a numeric value.&lt;br&gt;
The following code shows the comparison between a normal number and a separated number for one million:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const oneMillion = 1000000;
const separatedOneMillion = 1_000_000;
console.log(separatedOneMillion); //1000000

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

&lt;/div&gt;



&lt;p&gt;As you can see, separating the number with an underscore makes it much more readable. You can even use it on numbers with floating points as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const randomFloat = 4.11_857_111_1211;
console.log(randomFloat) //4.118571111211
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The separator won’t be included when you use the value for operations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const randomFloat = 4.7_857_123;
console.log(randomFloat); // 4.7857123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Imported Note that you can only use the separator between two digits:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const num = 4_111; // 4111
// All the below throws an error
const a = 47_; //Uncaught SyntaxError: Numeric separators are not allowed at the end of numeric literals
const b = _11;//Uncaught ReferenceError: _47 is not defined
const c= 7._11;Uncaught SyntaxError: Invalid or unexpected token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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