<?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: Radium Sharma</title>
    <description>The latest articles on DEV Community by Radium Sharma (@radiumsharma06).</description>
    <link>https://dev.to/radiumsharma06</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%2F57603%2Fc25a7d2c-369a-4291-b630-e56231ad10ea.png</url>
      <title>DEV Community: Radium Sharma</title>
      <link>https://dev.to/radiumsharma06</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/radiumsharma06"/>
    <language>en</language>
    <item>
      <title>ABC of Redux</title>
      <dc:creator>Radium Sharma</dc:creator>
      <pubDate>Wed, 21 Mar 2018 10:39:09 +0000</pubDate>
      <link>https://dev.to/radiumsharma06/abc-of-redux-5461</link>
      <guid>https://dev.to/radiumsharma06/abc-of-redux-5461</guid>
      <description>&lt;p&gt;Hello folks!&lt;/p&gt;

&lt;p&gt;Lately, React and Redux have been the talk of the town and I've been playing around with it for a while and learnt a few nuances. I am writing this so anyone who's getting started with Redux can see it from beginner's perspective.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux
&lt;/h2&gt;

&lt;p&gt;Redux has no dependency on react and vice-versa. They both work well together and do their respective roles.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React provides the &lt;em&gt;view&lt;/em&gt;&lt;/strong&gt; - &lt;strong&gt;Redux manages its &lt;em&gt;state&lt;/em&gt; logic&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux Terminologies
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;store&lt;/li&gt;
&lt;li&gt;state&lt;/li&gt;
&lt;li&gt;dispatch&lt;/li&gt;
&lt;li&gt;actions&lt;/li&gt;
&lt;li&gt;action creators&lt;/li&gt;
&lt;li&gt;reducers&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Redux Lifecycle
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.stack.imgur.com%2FLNQwH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.stack.imgur.com%2FLNQwH.png" alt="Redux Lifecycle" width="541" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above picture gives a good overview of how redux cycle works. &lt;/p&gt;

&lt;h4&gt;
  
  
  Core Redux principles
&lt;/h4&gt;


&lt;li&gt; It has a global state provider which is known as &lt;b&gt;store&lt;/b&gt; which contains the entire state logic of your application.This has a huge advantage in the sense that there is a single source of truth for state and it is globally accessible throughout your application i.e. in all components once it is in redux store.
For eg. We make an api call once and store the data in our redux store and then we can access the data in any of our components.I prefer using &lt;b&gt;redux saga&lt;/b&gt; for making api call through redux but lets discuss that in some later article.
&lt;b&gt;State&lt;/b&gt; data can be anything from some checking a radio button to large data coming from some API.


&lt;/li&gt;
&lt;li&gt;Next question is we have a state but how do we update or add to it? 
   Lets see how its done.

&lt;p&gt;A state should only be updated from dispatching an &lt;b&gt;action&lt;/b&gt; through &lt;b&gt;action creators&lt;/b&gt; (Remember the keywords I mentioned before)&lt;br&gt;
   Lets see what they are :-&lt;br&gt;
    Action is a simple javascript object which we dispatch or you can say launch in order to change a state. It will be better with an example. Lets say we have a label tag which has 'hello' written in it, we need to change it to 'bye' so how do we do it through redux. Our action will be something like this initially&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LABEL_VALUE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;And our react component will be something like&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;LabelComponent&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Label&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;labelValue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;changeLabel&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now we need to update its value on clicking a button so how do we do it?&lt;br&gt;
We dispatch an action on clicking of the button.&lt;br&gt;
Dispatching an action will be something like this :-&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;changeLabelOnClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newLabelValue&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="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;changeLabelValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bye&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;LabelComponent&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Label&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;labelValue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;changeLabelOnClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Note here changeLabelValue is nothing but an action creator which returns a new object or an action. Here's how changeLabelValue will look:-&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;changeLabelValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;labelValue&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LABEL_VALUE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;labelValue&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;If you notice action creator is just a wrapper over an action which is a good practice. You can dispatch an action directly which would look something like this&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LABEL_VALUE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;labelValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bye&lt;/span&gt;&lt;span class="dl"&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;p&gt;Now next question is once you dispatch an action what really happens and how state changes ?&lt;/p&gt;

&lt;p&gt;Lets see :- &lt;br&gt;
   As you guys would have noticed while dispatching an action object we added a 'type' key to it (type : 'LABEL_VALUE').This is a string which is the deciding factor as to which part of store will change and how it will change.&lt;br&gt;
&lt;b&gt;Reducers&lt;/b&gt; are the ones which subscribe to these types and change the state accordingly. The actual change in state occurs in the reducers. Lets see how:- &lt;/p&gt;

&lt;p&gt;Reducer file snippet:-&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;labelReducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="nx"&gt;action&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="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LABEL_VALUE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;labelValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&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;p&gt;This is a function in a reducer which returns a new state on getting called.&lt;br&gt;
This function is triggered when an action is dispatched with an action type which is present in this reducer. In this case the action type is &lt;b&gt;'LABEL_VALUE'&lt;/b&gt;.&lt;br&gt;
If you notice it returns a new object and does not change an existing one.This is known as immutablity where we destory and create a new state everytime there is a change.&lt;/p&gt;

&lt;p&gt;Now if you have followed till now you will notice we can change the label value with any string we want. We just need to dispatch an action with appropriate value. For eg. &lt;code&gt;dispatch( changeLabelValue('yipeee') )&lt;/code&gt; and we are done, the value will be updated.&lt;/p&gt;

&lt;p&gt;Important thing to note here is the how label changed, we saw above :-&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Label&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;labelValue&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Label&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;How this changes on dispatching an action? This is the beauty of react-redux.&lt;br&gt;
This Label is in a component - LabelComponent. Now this component uses &lt;b&gt;'labelValue'&lt;/b&gt; state of the store. Whenever there is a change in the redux with the labelValue this component will re render as it is subscribed to redux.So when this component is re-rendered state.labelValue is already updated.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt; Now one important thing to note here is we have to bind view to state.
All the above code snippets were shown in one file for understanding purpose but we need to separate view logic from container logic. We saw we had a LabelComponent before so it is a pure component as it just renders a view but it needs to have a separate container component which provides data or state data to this component from redux as props. 

&lt;p&gt;Lets see both these files with full code:-&lt;/p&gt;

&lt;p&gt;Pure Component file&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//LabelComponent.js - Pure Component&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;LabelComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;labelValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;changeLabelOnClick&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Label&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;labelValue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;changeLabelOnClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Container file&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//LabelComponent.container.js&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;connect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;changeLabelValue&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../../actions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;LabelComponent&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./LabelComponent&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;mapStateToProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;labelValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;labelValue&lt;/span&gt;
  &lt;span class="p"&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;mapDispatchToProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dispatch&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;changeLabelOnClick&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;labelValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;changeLabelValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;labelValue&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapStateToProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mapDispatchToProps&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;LabelComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Lets understand these two files and how data flows from here to corresponding actions and reducers.&lt;/p&gt;

&lt;p&gt;Lets decode &lt;b&gt;LabelComponent.container.js&lt;/b&gt; first :-&lt;br&gt;
Here we first import action creators we need in order to change state.&lt;br&gt;
After that you will notice two functions &lt;b&gt;mapStateToProps&lt;/b&gt; and &lt;b&gt;mapDispatchToProps&lt;/b&gt; These functions do very much what their name suggests.&lt;br&gt;
&lt;b&gt;mapStateToProps&lt;/b&gt; takes data from redux and provides it to our view component which is &lt;b&gt;LabelComponent&lt;/b&gt; in this case as you can see we use labelValue as prop in LabelComponent.&lt;/p&gt;

&lt;p&gt;Similar to this &lt;b&gt;mapDispatchToProps&lt;/b&gt; provides functions as props to view component which can provide data back to containers as &lt;b&gt;callbacks&lt;/b&gt;. Here in this case &lt;b&gt;changeLabelOnClick&lt;/b&gt; is a callback function which is provided as a prop to LabelComponent. After this data is availabel in container we dispatch an action and &lt;b&gt;data flows to reducer -&amp;gt; store  and back to view with updated state&lt;/b&gt;. Now lets see &lt;b&gt;LabelComponent.js&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Most of it is covered in previous file explanation. Here we first get the props from container ( we are using destructing in props ). Then I guess flow is pretty much clear. On click of button the labelValue flows back to container and the new existing labelValue is present in component as prop.&lt;/p&gt;

&lt;p&gt;Now these two files finally connect to each other through a very handy component -&lt;br&gt;
 &lt;b&gt;connect&lt;/b&gt;from 'react-redux'.We import LabelComponent in container and provide it with the state data as props by using connect module along with &lt;b&gt;mapstateToProps&lt;/b&gt; and &lt;b&gt;mapDispatchToProps&lt;/b&gt; and export it as a single component as you can see in file.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapStateToProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mapDispatchToProps&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;LabelComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;One last thing I did not show how the entire store is available to the app and how the app subscribes to redux changes.I am attaching a small snippet to give the overview :-&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;reducers&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./reducers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./components/App&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Router&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Provider&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;,
&lt;/span&gt;  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&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;p&gt;Consider this as the starting point of your react app which is rendered to Dom and has all child react components. Here we use certain very useful node_modules which help us in forming a bond between react and redux.You can check their detailed explanation in redux official documentation. Here &lt;b&gt;createstore&lt;/b&gt; binds the entire state into a single store and assigns it to a variable. If you notice reducers is nothing but a folder with different reducer files having part of state.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Provider&lt;/b&gt; is the other component to which we provide our entire store and it propogates the store to entire react app components and its children for it to be accessible.&lt;/p&gt;

&lt;p&gt;Now if all this still is a bit hazy thats all right. I am attaching a link to my github repo which has an end to end implementation of react with redux, &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/radiumsharma06/React-Redux-App" rel="noopener noreferrer"&gt;React-Redux-App&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope it helps you. Feel free to drop a comment if you have any question.&lt;/p&gt;

&lt;p&gt;Kudos and have a nice and productive day :) !!&lt;/p&gt;


&lt;/li&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to dockerize an application?</title>
      <dc:creator>Radium Sharma</dc:creator>
      <pubDate>Fri, 23 Feb 2018 10:37:18 +0000</pubDate>
      <link>https://dev.to/radiumsharma06/how-to-dockerize-an-application--1f7k</link>
      <guid>https://dev.to/radiumsharma06/how-to-dockerize-an-application--1f7k</guid>
      <description>&lt;p&gt;This article is mainly to show basic steps we need to follow in order to dockerize an application based on different tech stack. &lt;/p&gt;

&lt;p&gt;Lets start by dockerizing a nodejs application.&lt;br&gt;
First of all create a file named 'Dockerfile' without any extension in the parent directory of your project.&lt;/p&gt;

&lt;p&gt;This is a basic Dockerfile we need to dockerize a node application&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:4-onbuild
RUN mkdir /app
COPY . /app/
WORKDIR /app
RUN npm install
EXPOSE 8234
CMD [ "npm", "start" ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I will explain each command and what it does.&lt;br&gt;
If you look carefully you will notice that this Dockerfile is actually doing basic steps we do to run any nodejs application. &lt;/p&gt;

&lt;p&gt;Let us look at each command one by one.&lt;/p&gt;

&lt;h1&gt;
  
  
  1. FROM node:4-onbuild
&lt;/h1&gt;

&lt;p&gt;This command pulls/downloads a base image from docker hub which is a public hub for docker images.For running a node appication you need to install node in your system or for running java you need jdk same way we need to add a base node image in our docker environment.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. RUN mkdir /app
&lt;/h1&gt;

&lt;p&gt;In this command we make create an empty directory which will be our workin directory with the code files.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. COPY . /app/
&lt;/h1&gt;

&lt;p&gt;This command copies all files in current directory to the newly created app directory.Your Dockerfile should be in the parent directory of your project.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. WORKDIR /app
&lt;/h1&gt;

&lt;p&gt;Here we switch from current directory to the app directory where we will run our application.&lt;/p&gt;

&lt;h1&gt;
  
  
  5. RUN npm install
&lt;/h1&gt;

&lt;p&gt;This npm command is related to node application.When we copied all dependencies in step 3, our main file - package.json would have been copied.So running above command installs all dependencies from the file and creates a node_modules folder with mentioned node packages.&lt;/p&gt;

&lt;h1&gt;
  
  
  6. EXPOSE 8234
&lt;/h1&gt;

&lt;p&gt;This command is to expose a port we want our docker image to run on.&lt;/p&gt;

&lt;h1&gt;
  
  
  7. CMD [ "npm", "start" ]
&lt;/h1&gt;

&lt;p&gt;This is a command line operation to run a node application. It may differ based on projects.&lt;/p&gt;

&lt;p&gt;Now once we have our Dockerfile ready lets build an image out of it.&lt;br&gt;
Assuming you all have docker installed on your system lets follow some simple steps:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Navigate to directory containing Dockerfile.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the following command on your terminal:-&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t 'any image name' .
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This command will start executing steps in Dockerfile one by one.&lt;br&gt;
It will first look for the base image in your local docker repository if it            doesn't find it it will download the image from docker hub.&lt;br&gt;
Considering Dockerfile is correctly written your image will be built.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the following command to look for your image.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker images
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This command will list down all images in your local docker repo.Considering you gave your image name as hello-docker, you can see this name when you run the above command.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now we have image ready but we need a container to see our application running.A container is nothing but a running instance of your image.Run the following command to start an image or build a container.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -p 8080:8080 'your image name'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will create a container with your image.You can give port number of your choice.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now to after running the previous command we can go to the mentioned port and see our application running.To see the running container run the following command:-&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To see all containers in your docker run the following command:-&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps -a
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You will see containers with random names and ids.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To delete a container run following command:-&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker rm 'container name/ container id'
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To stop container run folloing :- &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker stop 'container name/ container id'
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To start container run following command:-&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker start '&amp;lt;container name/ container id&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You can also delete an image but make sure it doesn't have any containers.Run the following command:-&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker rmi 'your image name'  
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope this article helps in dockerizing a basic application there is lot more we could do with docker.&lt;/p&gt;

&lt;p&gt;Below is another Dockerfile which I created for a java project I was working on.You can easily relate it to previous Dockerfile.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM openjdk:8-jdk-alpine
RUN mkdir /apollo-services
COPY . /app
WORKDIR /app
RUN ./gradlew raml-generate
RUN ./gradlew clean build
WORKDIR /app/service-impl/build/libs
EXPOSE 8080
ENTRYPOINT ["java","-jar","main.jar"]        
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Thank you all and have a great day :) !!        &lt;/p&gt;

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