<?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: Garbage Value</title>
    <description>The latest articles on DEV Community by Garbage Value (@garbagevalue).</description>
    <link>https://dev.to/garbagevalue</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%2F472027%2Fc8fbdc33-8245-4c6d-972c-a5e72fdbb9f3.png</url>
      <title>DEV Community: Garbage Value</title>
      <link>https://dev.to/garbagevalue</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/garbagevalue"/>
    <language>en</language>
    <item>
      <title>DayJS: The best lightweight alternative to momentjs</title>
      <dc:creator>Garbage Value</dc:creator>
      <pubDate>Sun, 20 Sep 2020 09:18:45 +0000</pubDate>
      <link>https://dev.to/garbagevalue/dayjs-the-best-lightweight-alternative-to-momentjs-4o97</link>
      <guid>https://dev.to/garbagevalue/dayjs-the-best-lightweight-alternative-to-momentjs-4o97</guid>
      <description>&lt;p&gt;
  If you're reading this article, it means you've come out of a phase where
  you've started to look at the size of the package instead of installing it
  just because it is popular and do your job. In this article, I will compare
  &lt;strong&gt;&lt;a title="momentjs" href="https://momentjs.com/" rel="noopener nofollow noreferrer"&gt;momentjs&lt;/a&gt;
    vs
    &lt;a title="dayjs" href="https://day.js.org/" rel="noopener nofollow noreferrer"&gt;dayjs&lt;/a&gt;&lt;/strong&gt;
  in many aspects. And tell you how and when dayjs is better and when
  &lt;strong&gt;momentjs&lt;/strong&gt;.
&lt;/p&gt;

&lt;p&gt;
  A few days back I was optimizing the size of one of my old react projects, I
  saw moment import which I don't use nowadays, so I decided to write an article
  on this showing why you should consider &lt;strong&gt;dayjs&lt;/strong&gt; shouldn't use
  &lt;strong&gt;momentjs&lt;/strong&gt; just because you want to show date as
  &lt;em&gt;9 March, 20 &lt;/em&gt;instead of &lt;em&gt;09/03/2020&lt;/em&gt;.
&lt;/p&gt;

&lt;h2 id="why-momentjs-first-of-all-master-of-time"&gt;
   Why momentjs first of all? - Master of Time
  
&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%2Fik.imagekit.io%2Fgarbagevalue%2Ftr%3An-blog_in_content%2Fmaxresdefault_9icg3BAX4M.jpg" 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%2Fik.imagekit.io%2Fgarbagevalue%2Ftr%3An-blog_in_content%2Fmaxresdefault_9icg3BAX4M.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
  &lt;strong&gt;MomentJS&lt;/strong&gt; is one of the most popular and oldest date-related
  libraries. It has around
  &lt;a title="momentjs github" href="https://github.com/moment/moment/" rel="noopener nofollow noreferrer"&gt;43k GitHub
    stars&lt;/a&gt;
  and is being used by around 1.3m GitHub repositories. It is most popular
  because of its rich and simple APIs for
  &lt;a href="https://momentjs.com/docs/#/displaying/format/" rel="noopener nofollow noreferrer"&gt;date and time formatting and
    manipulation&lt;/a&gt;
  and multi-platform compatibility and if anyone needs date/time formatting or
  manipulation, &lt;strong&gt;momentjs&lt;/strong&gt; come first in mind. Maybe that is one
  of the reasons why everyone prefers momentjs in the first place, even in my
  company, they prefer momentjs as there's no alternative with such sweet APIs
  exist, and even exist it may not be popular as much as momentjs. So no one
  dares to think anything beyond momentjs. But there are reasons when one has to
  consider and look beyond. In the next section, let's see why momentjs became
  the point of debate and how the need for some
  &lt;strong&gt;alternative&lt;/strong&gt; felt.
&lt;/p&gt;

&lt;h2 id="why-one-shouldn-t-use-moment-js-then"&gt;
  Why one shouldn't use momentJS then?
&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%2Fik.imagekit.io%2Fgarbagevalue%2Ftime_H-6WZ3TzdZ.jpg%3Ftr%3Dn-lg" 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%2Fik.imagekit.io%2Fgarbagevalue%2Ftime_H-6WZ3TzdZ.jpg%3Ftr%3Dn-lg" alt="momentjs"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
    Photo by&lt;br&gt;
    &lt;a href="https://unsplash.com/@sonnnchen?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener nofollow noreferrer"&gt;Sonja&lt;br&gt;
      Punz&lt;/a&gt;&lt;br&gt;
    on&lt;a href="https://unsplash.com/?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener nofollow noreferrer"&gt;Unsplash&lt;/a&gt;&lt;br&gt;
  
  &lt;/p&gt;

&lt;p&gt;
  It's a trade-off, with such great features comes great &lt;strong&gt;cost&lt;/strong&gt;.
&lt;/p&gt;

&lt;p&gt;
  I'm using
  &lt;a title="VSCode Import Cost" href="https://marketplace.visualstudio.com/items?itemName=wix.vscode-import-cost" rel="noopener nofollow noreferrer"&gt;VSCode Import Cost&lt;/a&gt;
  extension to check the import cost of each library, and it is showing 136 KBs,
  which is the most expensive library I'm using right now, much more than
  &lt;a title="MaterialUI" href="https://material-ui.com/" rel="noopener nofollow noreferrer"&gt;MaterialUI&lt;/a&gt;
  that costs me 80 kBs. While the material-UI is the most used package. In every
  jsx/tsx file, you'll see at least one import from marterial-UI.&lt;br&gt;So the
  question is, is it worth it? Is it doing that much work that I pay more than
  130 kBs? Like some applications only needs to format the date while some
  application shows the time with respect to the current time, like showing the
  timing of some events, is it being tomorrow, today or is past, some bar is
  closed or when closing, etc. And some cases may also involve the timezone,
  which is a headache on its own.
  &lt;span&gt;The answer depends on application to application, and mostly we format the
    date, and a bit less frequently we do date manipulation.&lt;/span&gt;&lt;span&gt;
  &lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;
  &lt;span&gt;So for only these work, &amp;gt; 130 kBs is not a good deal, especially when
    your application is SEO demanding, everyone (me too) want to load the
    application faster, I have applied many tactics to do, and while optimizing
    the package size of my application, 130+ kBs caught my eyes. This has to be
    fixed.&lt;/span&gt;
&lt;/p&gt;

&lt;h2 id="if-not-moment-js-then-what-alternative"&gt;
  If not momentJS then what alternative?
&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%2Fik.imagekit.io%2Fgarbagevalue%2Ftr%3An-in_blog_content%2Fday-js_DIIhkzReR2.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%2Fik.imagekit.io%2Fgarbagevalue%2Ftr%3An-in_blog_content%2Fday-js_DIIhkzReR2.png" alt="Day-JS-Github-Cover"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
    Image Credit:&lt;a href="https://github.com/iamkun/dayjs" rel="noopener nofollow noreferrer"&gt;DayJS GitHub&lt;/a&gt;&lt;br&gt;
  
  &lt;/p&gt;

&lt;p&gt;
  I came to know about a library that mainly focused to tackle exactly the same
  issue that I mentioned above, named
  &lt;a title="Day JS" href="https://day.js.org/" rel="noopener nofollow noreferrer"&gt;DayJS&lt;/a&gt;. The first two lines on their
  official documentation are
&lt;/p&gt;

&lt;blockquote&gt;
  &lt;strong&gt;DayJS&lt;/strong&gt; &lt;br&gt;&lt;strong&gt;2kB JavaScript date utility library&lt;/strong&gt;
&lt;/blockquote&gt;

&lt;p&gt;They say,&lt;/p&gt;

&lt;blockquote&gt;
  Day.js is a minimalist JavaScript library that parses, validates, manipulates,
  and displays dates and times for modern browsers with a largely
  Moment.js-compatible API. If you use Moment.js, you already know how to use
  Day.js.
&lt;/blockquote&gt;

&lt;p&gt;
  &lt;a title="https://github.com/iamkun/dayjs" href="https://github.com/iamkun/dayjs" rel="noopener nofollow noreferrer"&gt;DayJS&lt;/a&gt;
  becoming popular with 25.6k GitHub stars surpassing
  &lt;a title="https://github.com/date-fns/date-fns" href="https://github.com/date-fns/date-fns" rel="noopener nofollow noreferrer"&gt;date-fns&lt;/a&gt;
  with 21.2k stars. I haven't used date-fns, so here I can't compare both. But
  of course some time. It is not only the size that makes it my choice, but also
  the APIs, that resembles momentjs. So it will be a very little pain for me to
  move from momentjs to dayjs. But just before moving, I made some comparisons
  for sizes and performance, as everyone says it is the best lib package in
  their documentation, but the reality comes when you start looking in the
  things yourself.
&lt;/p&gt;

&lt;h2 id="moment-js-vs-day-js"&gt;
  momentJS vs DayJS?
&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%2Fik.imagekit.io%2Fgarbagevalue%2Ftr%3An-blog_in_content%2Fdayjs-versus-momentjs_K9ZZSejiE.jpg" 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%2Fik.imagekit.io%2Fgarbagevalue%2Ftr%3An-blog_in_content%2Fdayjs-versus-momentjs_K9ZZSejiE.jpg" alt="momentjs-vs-dayjs"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
    Photo by &lt;a href="https://unsplash.com/@jsnbrsc?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener nofollow noreferrer"&gt;Jason&lt;br&gt;
      Briscoe&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/fight?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener nofollow noreferrer"&gt;Unsplash&lt;/a&gt;&lt;br&gt;
  
  &lt;/p&gt;

&lt;p&gt;
  So, for comparing momentJS with dayJS, I create three react projects using the
  command &lt;code&gt;npx
  &lt;a title="create-react-app" href="https://garbagevalue.com/definitions/cra-create-react-app" rel="noopener dofollow noreferrer"&gt;create-react-app&lt;/a&gt;&lt;/code&gt;
  with names &lt;em&gt;dayjs-example&lt;/em&gt; and &lt;em&gt;momentjs-example&lt;/em&gt; and
  &lt;em&gt;minimal&lt;/em&gt;. &lt;em&gt;dayjs-example&lt;/em&gt; is to test dayjs, and so on. minimal
  is just the empty
  &lt;a title="boilerplate" href="https://garbagevalue.com/definitions/boilerplate" rel="noopener dofollow noreferrer"&gt;boilerplate&lt;/a&gt; for
  reference to the other two projects.
&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;h3 id="import-cost"&gt;import cost&lt;/h3&gt;
    &lt;p&gt;
      I added import statements in the &lt;code&gt;App.js&lt;/code&gt; created
      by &lt;a title="create-react-app" href="https://garbagevalue.com/definitions/cra-create-react-app" rel="noopener dofollow noreferrer"&gt;React CLI&lt;/a&gt; boilerplate and
      just called &lt;code&gt;format&lt;/code&gt; function
      for both in their corresponding projects
      and &lt;code&gt;minimal&lt;/code&gt; is just the boilerplate, to compare how
      much each library causing the increase in size. 
    &lt;/p&gt;
    &lt;p&gt;
      Import costs for dayJS&lt;strong&gt; showed 7.2K (gzipped: 3K)&lt;/strong&gt; and for momentJS, it
      showed &lt;strong&gt;130.6K (gzipped: 48K). &lt;/strong&gt;Here they show a huge difference, but these are just
      import costs, it may
      differ at the time of building depends on how each library has implemented
      imports and working of webpack according to the functions used in our
      code.
    &lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;h3 id="bundle-size"&gt;Bundle Size&lt;/h3&gt;
    &lt;p&gt;
      Now next I just built all of the three projects to check how much they
      differ in the bundle size. And here is the result I got.
    &lt;/p&gt;
    &lt;h4 id="day-js"&gt;DayJS&lt;/h4&gt;
    &lt;p&gt;
      42.3 KB  2.1807f41e.chunk.js&lt;br&gt;778 B   
      runtime-main.950151df.js&lt;br&gt;633 B    main.1cb3d8cc.chunk.js&lt;br&gt;556
      B    main.d1b05096.chunk.css
    &lt;/p&gt;
    &lt;h4 id="moment-js"&gt;MomentJS&lt;/h4&gt;
    &lt;p&gt;
      56.24 KB  2.891328c7.chunk.js&lt;br&gt;&lt;span&gt;783 B     runtime-main.5092e59c.js&lt;br&gt;&lt;/span&gt;635 B   
       main.c2cbbad2.chunk.js&lt;br&gt;556 B   
       main.d1b05096.chunk.css
    &lt;/p&gt;
    &lt;h4 id="minimal-project-without-any-external-library-aside-from-boilerplate"&gt;
      Minimal Project (without any
        external library aside from
        boilerplate)
    &lt;/h4&gt;
    &lt;p&gt;
      39.84 KB  2.917d530d.chunk.js&lt;br&gt;768 B   
       runtime-main.bd245a24.js&lt;br&gt;608 B   
       main.a5ca775c.chunk.js&lt;br&gt;556 B   
       main.d1b05096.chunk.css
    &lt;/p&gt;
    &lt;p&gt;
      So here, moment caused an increase of approx 16.4K and dayjs caused 2.46K
      which is a huge difference again. Momentjs caused around 8 times more size
      than the dayjs. It may vary on how much functions you use in your code
      from both the libraries.
    &lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;h3 id="performance"&gt;Performance&lt;/h3&gt;
    &lt;p&gt;
      Now, I'll show my results of testing the format functions for both
      momentjs and dayjs. I installed both momentjs and dayjs in my
      &lt;em&gt;minimal&lt;/em&gt; project (size comparison is done). I wrote the following
      chunk of code in &lt;code&gt;App.js&lt;/code&gt; function
    &lt;/p&gt;
    &lt;pre&gt;&lt;code&gt;useEffect(() =&amp;gt; {
console.time('1');
moment().format('DD-MM-YYYY');
console.timeEnd('1');

console.time('2');
dayjs().format('DD-MM-YYYY');
console.timeEnd('2');

}, []);​&lt;/code&gt;&lt;/pre&gt;
    &lt;p&gt;and the output of the following results as follows&lt;/p&gt;
    &lt;pre&gt;&lt;code&gt;1: 2.2763671875ms
2: 1.260986328125ms​&lt;/code&gt;&lt;/pre&gt;
    &lt;p&gt;
      so it is clearly seen that dayjs took around half of the time of momentjs
      for the same function.
    &lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id="conclusion"&gt;Conclusion&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%2Fik.imagekit.io%2Fgarbagevalue%2Ftr%3An-blog_in_content%2Fconslusion_cBDHlNVmu.jpg" 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%2Fik.imagekit.io%2Fgarbagevalue%2Ftr%3An-blog_in_content%2Fconslusion_cBDHlNVmu.jpg" alt="momentjs-vs-dayjs-conclusion"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
    Photo by &lt;a href="https://unsplash.com/@kellysikkema?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener nofollow noreferrer"&gt;Kelly&lt;br&gt;
      Sikkema&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/end?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener nofollow noreferrer"&gt;Unsplash&lt;/a&gt;&lt;br&gt;
  
  &lt;/p&gt;


&lt;p&gt;&lt;br&gt;
  Now, the conclusion of the article is&lt;br&gt;
  &lt;strong&gt;dayjs is the lightweight alternative&lt;/strong&gt; to&lt;br&gt;
  momentjs. &lt;strong&gt;BUT &lt;/strong&gt;again, dayjs can't replace momentjs&lt;br&gt;
  completely, or we can say hasn't replaced yet. Momentjs offers a lot of other&lt;br&gt;
  things like timezone, which is not supported by dayjs yet. dayjs is a&lt;br&gt;
  &lt;strong&gt;good replacement of momentjs&lt;/strong&gt; for basic date transformations,&lt;br&gt;
  display, formatting, and so on. So it's all upon your requirements.&lt;br&gt;Thank&lt;br&gt;
  you for reading. I hope you enjoyed it, if I missed anything or has any&lt;br&gt;
  mistake in the &lt;strong&gt;comparison&lt;/strong&gt; of both libraries, please let me&lt;br&gt;
  know&lt;/p&gt;


&lt;p&gt;The article &lt;a href="https://garbagevalue.com/blog/dayjs-the-best-lightweight-alternative-to-momentjs" rel="noopener dofollow noreferrer"&gt;DayJS: The best lightweight alternative to momentjs&lt;/a&gt; was originally published on &lt;a href="https://garbagevalue.com/" rel="noopener dofollow noreferrer"&gt;Garbage Value&lt;/a&gt;&lt;/p&gt;

</description>
      <category>momentjs</category>
      <category>react</category>
      <category>javascript</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Use react hooks with react-redux in typescript</title>
      <dc:creator>Garbage Value</dc:creator>
      <pubDate>Sun, 20 Sep 2020 00:03:33 +0000</pubDate>
      <link>https://dev.to/garbagevalue/use-react-hooks-with-react-redux-in-typescript-42h5</link>
      <guid>https://dev.to/garbagevalue/use-react-hooks-with-react-redux-in-typescript-42h5</guid>
      <description>&lt;p&gt;
    It's been more than a year when
    &lt;a title="React" href="https://reactjs.org/" rel="noopener nofollow"&gt;&lt;strong&gt;React&lt;/strong&gt;&lt;/a&gt;
    introduced
    &lt;strong&gt;&lt;a href="https://reactjs.org/docs/hooks-intro.html" rel="noopener nofollow"&gt;hooks&lt;/a&gt;&lt;/strong&gt;. Many popular libraries that used with ReactJS like
    &lt;a title="React Router" href="https://reacttraining.com/react-router/web/" rel="noopener nofollow"&gt;React-Router&lt;/a&gt;,
    &lt;strong&gt;&lt;a title="React Redux" href="https://react-redux.js.org/" rel="noopener"&gt;React-Redux&lt;/a&gt;&lt;/strong&gt;,
    &lt;a title="Material UI" href="https://material-ui.com/" rel="noopener nofollow"&gt;Material UI&lt;/a&gt;, etc. now support hooks and &lt;strong&gt;functional components&lt;/strong&gt;. But
    today in this article, I will show you the new way of accessing store and
    dispatch in your functional component without using
    &lt;a title="higher order components" href="https://reactjs.org/docs/higher-order-components.html#___gatsby" rel="noopener nofollow"&gt;higher-order components&lt;/a&gt;, and compare how hooks reduced huge pile of
    &lt;a title="boilerplate" rel="noopener dofollow" href="https://garbagevalue.com/definitions/boilerplate"&gt;boilerplate&lt;/a&gt;.
&lt;/p&gt;




&lt;p&gt;
    Related Article:
    &lt;a title="Latest Features of Typescript 2020 release" href="https://garbagevalue.com/blog/latest-features-of-typescript-3-8-2020-release" rel="noopener dofollow"&gt;Latest Features of Typescript 2020 release&lt;/a&gt;
&lt;/p&gt;




&lt;h2 id="1-what-are-hooks-in-react-js"&gt;
    1. What are hooks in ReactJS?
&lt;/h2&gt;

&lt;p&gt;
    &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y7gECAHq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ik.imagekit.io/garbagevalue/what-are-react-hooks_ATUe2od3Z.jpg%3Ftr%3Dn-lg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y7gECAHq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ik.imagekit.io/garbagevalue/what-are-react-hooks_ATUe2od3Z.jpg%3Ftr%3Dn-lg" alt="What are react hooks?" width="100%" height="auto"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
    &lt;span&gt;Photo by &lt;/span&gt;&lt;a href="https://unsplash.com/@chuttersnap?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;chuttersnap&lt;/a&gt;&lt;span&gt; on &lt;/span&gt;&lt;a href="https://unsplash.com/s/photos/hook?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
    If you're aware of what are hooks in react, you can skip to the
    next section. 
&lt;/p&gt;

&lt;p&gt;
    React hook is a new concept introduced in
    &lt;a title="React 16.8" href="https://reactjs.org/blog/2019/02/06/react-v16.8.0.html" rel="noopener"&gt;React 16.8&lt;/a&gt;, that enables programmers the power to separate the logic from the UI. We
    all know that states used only in react component, so what if there's a logic
    bind with that state, and you want to use it in multiple files? You forced to
    return a JSX, which makes it a functional component. Now, that was the
    motivation.
&lt;/p&gt;

&lt;p&gt;
    So now, you can write a functional
    &lt;span&gt;component&lt;/span&gt; hook, you can do
    everything that you do in a functional component except you can return
    anything rather than just a JSX. Hooks name start with
    &lt;strong&gt;use&lt;/strong&gt; like &lt;code&gt;useEffect&lt;/code&gt;,
    &lt;code&gt;useState&lt;/code&gt; etc. It is not a strict rule, but it considered a
    standard to identify hooks.
&lt;/p&gt;

&lt;h2 id="2-react-redux-hooks-class-vs-functional-component-comparison"&gt;
    2. React redux hooks: Class vs Functional component comparison
&lt;/h2&gt;

&lt;p&gt;
    (See the complete code on
    &lt;a title="Code Sandbox" href="https://codesandbox.io/s/hooks-with-react-redux-example-using-with-typescript-to-reduce-the-react-boilerplate-3lu3b)" rel="noopener"&gt;Code Sandbox&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
    I'm writing a simple class component and a functional component that render
    the name of a user. First, we dispatch the name of the user, and next, we
    select the name from the store in the component to display it in the
    component.
&lt;/p&gt;

&lt;pre class="language-typescript"&gt;&lt;code&gt;import * as React from "react";
import { connect } from "react-redux";
import { TReduxStore, TDispatch } from "./types";
import { AxiosPromise } from "axios";
import { getUser } from "./action";

type IStateProps = Pick&amp;lt;TReduxStore, "user"&amp;gt;;

interface IDispatchProps {
  getUser: (id: string) =&amp;gt; AxiosPromise;
}

interface IProps extends IStateProps, IDispatchProps {}

class ShowUser extends React.Component&amp;lt;IProps&amp;gt; {
  componentDidMount() {
    this.props.getUser("1");
  }

  render() {
    const { user } = this.props;
    return !user ? "Loading..." : &amp;lt;div&amp;gt;User Name: {user.name}&amp;lt;/div&amp;gt;;
  }
}

const mapStateToProps = (state: TReduxStore): IStateProps =&amp;gt; ({
  user: state.user
});

const mapDispatchToProps = (dispatch: TDispatch): IDispatchProps =&amp;gt; ({
  getUser: id =&amp;gt; dispatch(getUser(id))
});

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ShowUser);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
    Above is the
    &lt;a href="https://react-redux.js.org/api/connect" rel="noopener"&gt;traditional way of connecting your ReactJS component with the redux&lt;/a&gt;. You have to write that much code just to request and display the user name.
    Now let's see the same thing in the functional component.
&lt;/p&gt;

&lt;pre class="language-typescript"&gt;&lt;code&gt;import * as React from "react";
import { useSelector, useDispatch } from "react-redux";
import { TReduxStore, TDispatch } from "./types";
import { getUser } from "./action";

type IStateProps = Pick&amp;lt;TReduxStore, "user"&amp;gt;;

const ShowUser: React.FC = props =&amp;gt; {
  const dispatch = useDispatch&amp;lt;TDispatch&amp;gt;();

  const { user } = useSelector&amp;lt;TReduxStore, IStateProps&amp;gt;(state =&amp;gt; ({
    user: state.user
  }));

  React.useEffect(() =&amp;gt; {
    dispatch(getUser("1"));
  }, [dispatch]);

  return !user ? &amp;lt;div&amp;gt;"Loading..."&amp;lt;/div&amp;gt; : &amp;lt;div&amp;gt;User Name: {user.name}&amp;lt;/div&amp;gt;;
};

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

&lt;p&gt;
    As you can see, using hooks, reduced the number of lines to almost half. I'm
    considering you're using the class component, so I'm not explaining that
    snippet. So I'm just explaining the FunctionalComponent.tsx below.
&lt;/p&gt;

&lt;h2 id="3-how-to-use-use-dispatch-and-use-selector-react-redux-hooks-in-your-functional-component"&gt;
3. How to use useDispatch and useSelector react-redux hooks in your functional component?
&lt;/h2&gt;

&lt;p&gt;
    Let's see one by one how to use the
    &lt;strong&gt;&lt;a title="use of useDispatch hook" href="https://react-redux.js.org/api/hooks#usedispatch" rel="noopener"&gt;useDispatch&lt;/a&gt;&lt;/strong&gt;
    and the
    &lt;a title="use of useSelector hook" href="https://react-redux.js.org/api/hooks#useselector-examples" rel="noopener"&gt;&lt;strong&gt;useSelector&lt;/strong&gt;&lt;/a&gt;
    to dispatch and select the data from the redux store.
&lt;/p&gt;

&lt;p&gt;
    &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yvkq6ykl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ik.imagekit.io/garbagevalue/use-dispatch-and-use-selector_ng2RpmM1L.jpg%3Ftr%3Dn-lg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yvkq6ykl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ik.imagekit.io/garbagevalue/use-dispatch-and-use-selector_ng2RpmM1L.jpg%3Ftr%3Dn-lg" alt="How to use useDispatch and useSelector in react redux typescript" width="100%" height="auto"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
    &lt;span&gt;Photo by &lt;/span&gt;&lt;a href="https://unsplash.com/@chris_j_scott?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Chris Scott&lt;/a&gt;&lt;span&gt; on &lt;/span&gt;&lt;a href="https://unsplash.com/s/photos/hook?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;
&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;
        &lt;h3 id="use-dispatch"&gt;useDispatch&lt;/h3&gt;
        &lt;p&gt;
            useDispatch hook used to get the &lt;strong&gt;redux&lt;/strong&gt; dispatch function
            in your functional component or hook. It takes a
            &lt;a title="generic type" href="https://www.typescriptlang.org/docs/handbook/generics.html" rel="noopener"&gt;generic type&lt;/a&gt;
            of data dispatch, takes in its parameters. Usually, it is
            &lt;code&gt;{type: string, payload?: any}&lt;/code&gt; but as I'm redux-thunk here, so
            its type is
            &lt;code&gt;ThunkDispatch&amp;lt;TReduxStore, {}, TAction&amp;gt;;&lt;/code&gt; where
            &lt;code&gt;TReduxStore&lt;/code&gt; is the type of the store and
            &lt;code&gt;TAction&lt;/code&gt; is &lt;code&gt;{type: string, payload?: any}&lt;/code&gt;.
        &lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;h3 id="use-selector"&gt;useSelector&lt;/h3&gt;
        &lt;p&gt;
            As its name suggests, it gives you the functionality of selecting the data
            from your &lt;strong&gt;redux store&lt;/strong&gt;. It takes one parameter, which is a
            callback, where it passes the whole &lt;strong&gt;redux&lt;/strong&gt; state, and you
            return the data you want to select from it. So the callback function looks
            like below.
        &lt;/p&gt;
        &lt;pre class="language-typescript"&gt;&lt;code&gt;function (state) {
    return ({
        user: state.user
    })
}&lt;/code&gt;&lt;/pre&gt;
        &lt;p&gt;
            The same thing I have written is in the flat arrow function format in the
            functional component. It takes two types in its
            &lt;a title="generic type" href="https://www.typescriptlang.org/docs/handbook/generics.html" rel="noopener"&gt;generic type&lt;/a&gt;. One is the type of your redux store, and the second is the type you
            want to select from the store. IN my codes, it is represented by
            &lt;code&gt;TReduxStore&lt;/code&gt; and &lt;code&gt;IStateProps&lt;/code&gt; respectively.
        &lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;





&lt;p&gt;The post &lt;a href="https://garbagevalue.com/blog/react-redux-hooks-in-typescript#table-of-content" rel="noopener dofollow"&gt;Use react hooks with react-redux in typescript&lt;/a&gt; was originally published on &lt;a href="https://garbagevalue.com/" rel="noopener dofollow"&gt;garbagevalue.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>redux</category>
    </item>
  </channel>
</rss>
