<?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>Forem: Shubham</title>
    <description>The latest articles on Forem by Shubham (@shubhamforu).</description>
    <link>https://forem.com/shubhamforu</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%2F399145%2F8c5f4e68-fb31-4c00-b509-4b33fc231038.jpeg</url>
      <title>Forem: Shubham</title>
      <link>https://forem.com/shubhamforu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shubhamforu"/>
    <language>en</language>
    <item>
      <title>How to create a custom dialog in React to show before user leave</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Fri, 01 Jan 2021 12:06:37 +0000</pubDate>
      <link>https://forem.com/shubhamforu/how-to-create-a-custom-dialog-in-react-to-show-before-user-leave-36b0</link>
      <guid>https://forem.com/shubhamforu/how-to-create-a-custom-dialog-in-react-to-show-before-user-leave-36b0</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2ZX5iIFX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7vbez1sxvkl1vvz3ur8e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2ZX5iIFX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7vbez1sxvkl1vvz3ur8e.png" alt="Custom dialog"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hello there, Today I am going to show a little trick in React that &lt;strong&gt;how you can create your custom own dialog to ask the user before they switch route(instead of native one)&lt;/strong&gt; when some value required some action (for ex: to save that form, to do some action based on that change)&lt;/p&gt;

&lt;h3&gt;
  
  
  How it comes?
&lt;/h3&gt;

&lt;p&gt;I was assigned one module where I have to prompt custom dialog when user have something changed into there form and they try to leave the site without saving those changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  My findings
&lt;/h3&gt;

&lt;p&gt;To be honest, Initially, I thought it might be easy to do as it is a simple dialog and just has to maintain some state to show/hide prompt. But the issue came when I have to track route changes also. After searching google I came to know about &lt;a href="https://stackoverflow.com/a/276739/11511722"&gt;this&lt;/a&gt; but since we had a requirement to show the custom dialog. Since &lt;code&gt;onBeforeUnload&lt;/code&gt; won't allow customizing its dialog(you can only change text string). After exploring not exact solution I have found that some packages are doing this.&lt;br&gt;
But we(my team) don't want to use any third-party library for that. So I got the chance to research that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;After exploring more on &lt;code&gt;google&lt;/code&gt; and &lt;code&gt;StackOverflow&lt;/code&gt; came to know about &lt;code&gt;react-router&lt;/code&gt; provide &lt;a href="https://reactrouter.com/web/api/history"&gt;history&lt;/a&gt; prop which can be utilized for that requirement. Then I started trying implementing it after going through their docs. And it was easy as I thought.&lt;br&gt;
So basically you need to follow these steps to do that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create your dialog component&lt;/li&gt;
&lt;li&gt;From wherever you want to block your route. Use &lt;code&gt;history.block&lt;/code&gt; on the component mount (This will block your navigation)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="nx"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unblock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tx&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Navigation was blocked! Let's show a confirmation dialog&lt;/span&gt;
      &lt;span class="c1"&gt;// so the user can decide if they actually want to navigate&lt;/span&gt;
      &lt;span class="c1"&gt;// away and discard changes they've made in the current page.&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Since &lt;code&gt;history.block&lt;/code&gt; gives you a callback that means you can unblock navigation by executing that
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;    &lt;span class="nx"&gt;mySomeRandomFunction&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unblock&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//If condition is satisfied&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;So once criteria are satisfied unblock that route. That's it you have to do only that&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Last but not the least (DEMO)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/custom-modal-route-block-bmqci?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark&amp;amp;view=preview"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pzpI10jk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codesandbox.io/static/img/play-codesandbox.svg" alt="Edit custom-modal-route-block"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Caveat
&lt;/h3&gt;

&lt;p&gt;According to &lt;code&gt;history&lt;/code&gt; documentation:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;history.block&lt;/code&gt; will call your callback for &lt;code&gt;all in-page navigation attempts&lt;/code&gt;, but&lt;br&gt;
for &lt;em&gt;navigation that reloads the page&lt;/em&gt; (e.g. the refresh button or a link that&lt;br&gt;
doesn't use &lt;code&gt;history.push&lt;/code&gt;) it registers a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event"&gt; &lt;code&gt;beforeunload&lt;/code&gt;&lt;br&gt;
handler&lt;/a&gt;&lt;br&gt;
to prevent the navigation. &lt;em&gt;In modern browsers you are not able to customize this&lt;br&gt;
dialog&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Keep above thing in mind i.e &lt;code&gt;it wont work if page is reload it only work if you try to change some action in page&lt;/code&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>hooks</category>
    </item>
    <item>
      <title>How to automate image compression with Git Hooks</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sat, 12 Sep 2020 17:06:07 +0000</pubDate>
      <link>https://forem.com/shubhamforu/how-to-automate-image-compression-with-git-hooks-1h5i</link>
      <guid>https://forem.com/shubhamforu/how-to-automate-image-compression-with-git-hooks-1h5i</guid>
      <description>&lt;p&gt;This story is a kind of story and tutorial mix. It consists of the following parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Why do we need to compress images?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What leads to me this?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How I did it?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So without delay let’s get started. &lt;strong&gt;WHY WHAT AND HOW :-)&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why do we need to compress images?
&lt;/h3&gt;

&lt;p&gt;This tutorial is not about why we need to compress images. There are already a ton of good resources over the internet of this. In summary: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Large images&lt;/strong&gt; slow down your web pages which creates a bad user experience. (no one wants to wait)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Large image&lt;/strong&gt; files slow down your site and search engine hates slow sites(it leads to bad SEO)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Large images&lt;/strong&gt; required high bandwidth&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Uncompressed&lt;/strong&gt; images bloat your pages with unnecessary bytes&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s it there is a lot more information about this. But there is no point in explaining here. You can find all this information on this internet.&lt;/p&gt;

&lt;h3&gt;
  
  
  What leads to me this?
&lt;/h3&gt;

&lt;p&gt;Whenever I am doing my personal project or in my organization. I need to organize my images and have to go to manually some compressing site and then convert it into a compressed one and replaced. My lead asked me one day why don’t we automate this stuff. I don’t find any good source or anything to automate this. So I thought It’s worth sharing also.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: There are already 3&lt;sup&gt;rd&lt;/sup&gt; party services that done this for you. But again you have to buy that service. This tutorial is all about automating compression using hooks and for smaller project you dont want your project to blot another 3&lt;sup&gt;rd&lt;/sup&gt; party service.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How I did it?
&lt;/h3&gt;

&lt;p&gt;This is the interesting part. Let’s start it. Initially the images size is this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A20rR9sN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d639kbd2o6y0wu8swmb7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A20rR9sN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d639kbd2o6y0wu8swmb7.png" alt="Before compress"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I took this image for demo purpose only&lt;/p&gt;

&lt;p&gt;So for automating this stuff I use &lt;a href="https://www.npmjs.com/package/husky"&gt;husky&lt;/a&gt;(for git hooks) and lint-staged.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Husky is a good npm package for using git hooks in a better way.&lt;/li&gt;
&lt;li&gt;lint-staged is a linter that runs for your staging file (it's like a code you want to run for staging file like beautifying your code, check unused code or malicious code, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For compression of images I use &lt;a href="https://www.npmjs.com/package/sharp"&gt;sharp&lt;/a&gt; (it’s open-source). If you have this question why this package why not other packages there are so many good packages. I totally agree with this. But all this question is already answered sharp. &lt;/p&gt;

&lt;p&gt;You can check its &lt;a href="https://sharp.pixelplumbing.com/performance"&gt;performance guide&lt;/a&gt;. It’s already giving the answer to those questions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code time:
&lt;/h3&gt;

&lt;p&gt;So logic is that before committing to the code check of images and if images find then compress them before committing. &lt;br&gt;
You can do other things (like post commit, pre build etc).&lt;/p&gt;

&lt;p&gt;Here is complete code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"hooks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"pre-commit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"lint-staged"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;








&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
  A precommit script to compress image before commiting file
  Q: Why Sharp package used?
  A: https://sharp.pixelplumbing.com/performance

 */&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sharp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sharp&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;//https://sharp.pixelplumbing.com/&lt;/span&gt;

&lt;span class="cm"&gt;/*
Function: Update existing file to new compress file
 */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;minifyFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/*Read upcomimg file*/&lt;/span&gt;
    &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sourceData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="cm"&gt;/*If file buffer data is present convert it into new compressed file*/&lt;/span&gt;
      &lt;span class="nx"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sourceData&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cm"&gt;/*
Fetch images maps from args and compress all.
Compressing is asynchronuous process.
So wait for all image to compress and return.
*/&lt;/span&gt;
&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="cm"&gt;/*Find more. here: https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/*/&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;minifyFile&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;








&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"image-compression"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Pre commit script to compress images"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"keywords"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Shubham Verma"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ISC"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"husky"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^4.2.5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"lint-staged"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^10.2.7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"sharp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^0.25.3"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lint-staged"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"*.{png,jpeg,jpg,gif,svg}"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"node ./compress-image.js"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I already explained everything in code. Feel free to reach me out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Last but not least proof ;-)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A1wVz5ff--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z5ibu3x31cvuwrluqxj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A1wVz5ff--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z5ibu3x31cvuwrluqxj1.png" alt="After compression"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s all folks. Feel free to ask a question if you find any doubt. The complete code is available on Github. Feel free to &lt;a href="https://github.com/shubhamV123/compress-image-script"&gt;checkout&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can reach me at twitter &lt;a href="https://twitter.com/shubham2133"&gt;@shubham2133&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>automation</category>
      <category>webdev</category>
      <category>git</category>
    </item>
    <item>
      <title>Formatting date phrases becomes easy in javascript</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sun, 30 Aug 2020 16:19:53 +0000</pubDate>
      <link>https://forem.com/shubhamforu/formatting-date-phrases-becomes-easy-in-javascript-49ng</link>
      <guid>https://forem.com/shubhamforu/formatting-date-phrases-becomes-easy-in-javascript-49ng</guid>
      <description>&lt;p&gt;Modern web applications often use phrases like “yesterday”, “42 seconds ago”, or “in 3 months” instead of full dates and timestamps. There is no direct solution given by javascript until now. You have to write manually function which is cumbersome in itself or rely on any third party library like &lt;a href="https://momentjs.com/" rel="noopener noreferrer"&gt;&lt;strong&gt;moment.js&lt;/strong&gt;&lt;/a&gt; etc. But you don't have to rely on any third party library to achieve this.&lt;/p&gt;

&lt;p&gt;The brand new Intl.RelativeTimeFormat API shifts that burden to the JavaScript engine, which can ship the locale data and make it directly available to JavaScript developers. Intl.RelativeTimeFormat enables localized formatting of relative times without sacrificing performance.&lt;/p&gt;

&lt;p&gt;These are some examples to get you better idea :-)&lt;/p&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AH7S1h02_aurC9H4DvxQ3aw.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AH7S1h02_aurC9H4DvxQ3aw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not only this you can convert these into your native language. For example :&lt;/p&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2ApCkjU4xX0TLcC1r8Mxl5lw.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2ApCkjU4xX0TLcC1r8Mxl5lw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s all from my side. Congrats yourself for learning new thing. You can explore more on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat" rel="noopener noreferrer"&gt;mdn&lt;/a&gt; docs.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>productivity</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Understanding Formik hooks with use case(complex)</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sun, 31 May 2020 16:46:47 +0000</pubDate>
      <link>https://forem.com/shubhamforu/understanding-formik-hooks-with-use-case-complex-438d</link>
      <guid>https://forem.com/shubhamforu/understanding-formik-hooks-with-use-case-complex-438d</guid>
      <description>&lt;p&gt;I was working on typical general problem thought worth to share. I am sure if you are working on forms using Formik so you come to this problem definitely. In this article I am sharing via Formik hooks. So I came to know &lt;a href="https://jaredpalmer.com/formik/docs/overview" rel="noopener noreferrer"&gt;Formik&lt;/a&gt; hooks they are amazing :-)&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Problem Statement&lt;/strong&gt;
&lt;/h3&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%2Fcdn-images-1.medium.com%2Fmax%2F791%2F1%2AjaDRP2mgYrG5bDrXDgFSeQ.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%2Fcdn-images-1.medium.com%2Fmax%2F791%2F1%2AjaDRP2mgYrG5bDrXDgFSeQ.png"&gt;&lt;/a&gt;&lt;strong&gt;Problem Statement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As diagram is self explanatory. I would like to explain if you don’t understand. Considering your manager/ designer or whoever ask you to create a form with some design where you have to create Form with accordion i.e Different form can generated based on which accordion we selected. For simplicity I am explaining for one But it can work for for anyone. How we can submit the form by clicking the submit button? And we need to send the value of form which we selected. Hope you got the context. Let’s start !&lt;/p&gt;

&lt;p&gt;After going through google and documentation what take my interest is Formik hook it make this thing very simple. Lets understand&lt;/p&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AHquhWy-886p-mz5HZCF0bg.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AHquhWy-886p-mz5HZCF0bg.png"&gt;&lt;/a&gt;&lt;strong&gt;Formik hook initialise&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In above code we are doing:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialising form variable
&lt;/li&gt;
&lt;li&gt;Assigning what to do when &lt;strong&gt;form is submit(onSubmit)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Last but not least assigning &lt;strong&gt;useFormik&lt;/strong&gt; reference to &lt;strong&gt;formik variable&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we can use this formik variable to do anything we want(like validation from parent or child component or submitting from parent or child component). &lt;strong&gt;Note&lt;/strong&gt; : You can do more things from this variable.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A0jOOGBzinyAY4ZGJz1GD9A.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A0jOOGBzinyAY4ZGJz1GD9A.png"&gt;&lt;/a&gt;Compelete code&lt;/p&gt;

&lt;p&gt;If you don’t understand from above snippet. Its okay. Here you can find live &lt;a href="https://codesandbox.io/s/formik-hooks-ktk0d" rel="noopener noreferrer"&gt;link&lt;/a&gt; and you can tinker around with it. I have added all the comments to make things easier for you. If you still don’t understand you can ask in comment section. That’s all for today guys.&lt;/p&gt;

&lt;p&gt;Demo of this code hosted &lt;a href="https://codesandbox.io/s/formik-hooks-ktk0d" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Feel free to check out.&lt;/p&gt;

&lt;p&gt;Thanx for making it to the end!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you like this article consider buying me a&lt;/strong&gt; &lt;a href="https://www.buymeacoffee.com/Shubhamforu" rel="noopener noreferrer"&gt;&lt;strong&gt;coffee&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;. Cheers!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>formik</category>
      <category>hooks</category>
      <category>react</category>
    </item>
  </channel>
</rss>
