<?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: ‌</title>
    <description>The latest articles on Forem by ‌ (@character8346).</description>
    <link>https://forem.com/character8346</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%2F460153%2Fb9a979a6-367b-46f2-9a28-e6b3e5efaed1.png</url>
      <title>Forem: ‌</title>
      <link>https://forem.com/character8346</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/character8346"/>
    <language>en</language>
    <item>
      <title>How to Get the Root State type from your TypeScript React application.</title>
      <dc:creator>‌</dc:creator>
      <pubDate>Wed, 09 Sep 2020 06:57:55 +0000</pubDate>
      <link>https://forem.com/character8346/how-to-get-the-root-state-type-from-your-typescript-react-application-5hah</link>
      <guid>https://forem.com/character8346/how-to-get-the-root-state-type-from-your-typescript-react-application-5hah</guid>
      <description>&lt;p&gt;One of the primary benefits of TypeScript is its static type checking, ensuring that the variables in your code are receiving the right things.&lt;/p&gt;

&lt;p&gt;There are many instances in your application where you need to grab the type of the state, especially if you are using Redux and combining different state reducers into one root reducer.&lt;/p&gt;

&lt;p&gt;Here is a quick guide to ensuring you always have the latest type of your state in your TypeScript React application!&lt;/p&gt;

&lt;h1&gt;
  
  
  Where Your Root State Lives in Redux
&lt;/h1&gt;

&lt;p&gt;For those using redux, the state of the application doesn't live inside components necessarily, which is the case normally. Rather your components are connected to a 'store' of the global application state and get their information from that.&lt;/p&gt;

&lt;p&gt;In most cases, you will have many 'reducers' that reduce certain state change actions, and these reducers will combine into a 'root reducer', that will combine all the states. It will look something like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rootReducer.ts&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 { combineReducers } from 'redux';

import userReducer from './..';
import shopReducer from './..';

const rootReducer = combineReducers({
    user: userReducer,
    shop: shopReducer
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Grabbing the State from the Root Reducer
&lt;/h1&gt;

&lt;p&gt;If you remember, the rootReducer describes the state of your redux application. To grab the type, simply write the following export:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export type RootState = ReturnType&amp;lt;typeof rootReducer&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;export type&lt;/code&gt;: so you can use the state type in other places&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RootState&lt;/code&gt;: name of the type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ReturnType&amp;lt;&amp;gt;&lt;/code&gt;: returns the type of the return inside the &amp;lt;&amp;gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;typeof rootReducer&lt;/code&gt;: the complicated redux type of the reducer that we will let TypeScript decode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The benefit of this is that the type is not fixed; you can add or remove your reducers from the root reducer and your root state will update accordingly!&lt;/p&gt;

&lt;p&gt;Thanks for reading :)&lt;/p&gt;

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