<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[SalvadorLekan]]></title><description><![CDATA[SalvadorLekan]]></description><link>https://bay.salvadorlekan.com.ng</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 11:09:39 GMT</lastBuildDate><atom:link href="https://bay.salvadorlekan.com.ng/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Zuri.team, my experience so far.]]></title><description><![CDATA[It's been a week, more accurately ten (10) days since I accepted the invite to join the Zuri Slack workspace for a four (4) months beginner training in tech.
Zuri
To those who don't know about Zuri, Zuri is an initiative that aims to train beginners ...]]></description><link>https://bay.salvadorlekan.com.ng/zuriteam-my-experience-so-far</link><guid isPermaLink="true">https://bay.salvadorlekan.com.ng/zuriteam-my-experience-so-far</guid><dc:creator><![CDATA[salvador abdurrahman]]></dc:creator><pubDate>Wed, 24 Mar 2021 09:27:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1616582705857/XR0EPar0L.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>It's been a week, more accurately ten (10) days since I accepted the invite to join the Zuri Slack workspace for a four (4) months beginner training in tech.</p>
<h2 id="zuri">Zuri</h2>
<p>To those who don't know about Zuri, <a target="_blank" href="https://zuri.team">Zuri</a> is an initiative that aims to train beginners in tech to be familiar with enough knowledge and concepts to be able to progress to the level of a junior developer or designer depending on the track one registers for.</p>
<p>Zuri also has plans for internships and talent placement in the nearest future.</p>
<h2 id="zuri-x-i4g">Zuri X I4G</h2>
<p>The present cohort of Zuri training is sponsored by <a target="_blank" href="https://ingressive.org/">Ingressive for Good</a> both who share similar vision to ensure that the training progress successfully.</p>
<h2 id="my-thoughts">My Thoughts</h2>
<p>The Slack workspace currently has close to nineteen thousand (19,000) individuals who are mostly trainees. Here are my thoughts on the programme so far.</p>
<ol>
<li>It is a very good initiative.</li>
<li>It is well structured.</li>
<li>It is rowdy.</li>
</ol>
<h3 id="it-is-a-very-good-initiative">It Is A Very Good Initiative</h3>
<p>The decision to train people in multitudes for free is not an easy one to come to. And I look forward to a successful running of this cohort.</p>
<h3 id="it-is-well-structured">It Is Well Structured</h3>
<p>Free programmes of this magnitude or less usually have a theme in common, only a select few know what is actually going on. But I'm glad it's not the case in this one as there is a functioning announcement channel, there are posts on the Learning Management Service (LMS). </p>
<p>Overall, I'm happy with the communication so far.</p>
<h3 id="it-is-rowdy">It Is Rowdy</h3>
<p>This may seem to contradict my last thought but it doesn't.</p>
<p>As expected from a programme of this magnitude, control over the trainees as at the time of writing this article seem non existent because the notifications from the workspace is overwhelming which prompted me to mute every channel except for the announcement channel.</p>
<p>Not to talk of how Slack's UI jumps around for few seconds after people start reacting to posts 😵.</p>
<h2 id="conclusions">Conclusions</h2>
<p>It's a training I will recommend for people to join.</p>
]]></content:encoded></item><item><title><![CDATA[React-Redux, the basic basic.]]></title><description><![CDATA[While learning React, you probably have heard people talk about redux and you got curious to find out what the fuss is about.
Chances are you've gone through tons of tutorials and articles but you only end up copy-pasting lines of code without unders...]]></description><link>https://bay.salvadorlekan.com.ng/react-redux-the-basic-basic</link><guid isPermaLink="true">https://bay.salvadorlekan.com.ng/react-redux-the-basic-basic</guid><category><![CDATA[React]]></category><category><![CDATA[Redux]]></category><dc:creator><![CDATA[salvador abdurrahman]]></dc:creator><pubDate>Sun, 16 Aug 2020 21:58:31 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1597487902536/j2LHjS_Xf.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1597485378741/_F-UXwNKr.jpeg" />
While learning React, you probably have heard people talk about redux and you got curious to find out what the fuss is about.
Chances are you've gone through tons of tutorials and articles but you only end up copy-pasting lines of code without understanding what is going on. </p>
<blockquote>
<p>Note: As at the time of writing this article, I am a learner and I am open to correction(s) on misrepresentation(s) of concepts discussed in this article or any future article. Thank you for understanding.</p>
</blockquote>
<h2 id="table-of-content">Table of content</h2>
<ul>
<li><a class="post-section-overview" href="#introduction-section">Introduction</a><ul>
<li><a class="post-section-overview" href="#WhatisRedux">What Is Redux?</a></li>
<li><a class="post-section-overview" href="#WhyReduxInReact">Why Redux In React?</a></li>
<li><a class="post-section-overview" href="#ReactAppSetup">React App Setup</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#redux-proper">Redux Proper</a><ul>
<li><a class="post-section-overview" href="#redux-basic-structure">Redux Basic Structure</a><ul>
<li><a class="post-section-overview" href="#action-section">Actions</a></li>
<li><a class="post-section-overview" href="#reducer-section">Reducers</a></li>
<li><a class="post-section-overview" href="#store-section">Store</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#coding-proper">Coding proper</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#summary-section">Summary</a></li>
</ul>
<p><span id="introduction-section"></span></p>
<h2 id="introduction">Introduction</h2>
<p></p>
<p><span id="WhatisRedux"></span></p>
<h3 id="what-is-redux-">What Is Redux?</h3>
<p></p>
<p>That is the first major question that needs an answer. Who is in a better position to answer other than <a href="https://redux.js.org/">Redux</a>.
As stated in their website, Redux is "A Predictable State Container for JS Apps".  To put it in another way, It is a state management service that works for any JavaScript application and it maintains consistency in its state management patterns, thus predictable.</p>
<p><span id="WhyReduxInReact"></span></p>
<h3 id="why-redux-in-react-">Why Redux In React?</h3>
<p>
In a typical React application, data is passed top-down (parent to child) via props. For simple applications, this may not be a major issue but when it comes to a fairly large application, passing state through numerous components will cause unnecessary re-rendering of components which affects performance.</p>
<p>There are already several ways to maintain and pass down states in React without passing through several components.
So why bother with Redux? As a beginner, a reason I can think of is that, Redux is not tied to React so its strength can be utilized in other JavaScript libraries and frameworks.</p>
<p>With this introduction, let's get started.</p>
<p><span id="ReactAppSetup"></span></p>
<h3 id="react-app-setup">React App Setup</h3>
<p></p>
<p>Let's setup a react app where we will be integrating redux. I will name the folder reactredux.
Change directory to a suitable folder and run the code below in the node terminal </p>
<pre><code><span class="hljs-attribute">npx</span> create-react-app reactredux
</code></pre><p>If it did not throw an error, a folder named reactredux ( or whatever you chose to name yours) should be present.
In the terminal run the code below to change directory into the reactredux folder.</p>
<pre><code><span class="hljs-built_in">cd</span> reactredux
</code></pre><p>Now that you are in the folder run the code below to add redux as a dependency</p>
<pre><code>npm <span class="hljs-keyword">install</span> redux react-redux <span class="hljs-comment">--save</span>
</code></pre><p>If you have VsCode installed run the code below, otherwise open the folder in a code editor of your choice.</p>
<pre><code><span class="hljs-attribute">code</span> .
</code></pre><p>After the command runs, hurray 🎉you are now set to use Redux.</p>
<p><span id="redux-proper"></span></p>
<h2 id="redux-proper">Redux Proper</h2>
<p></p>
<p><span id="redux-basic-structure"></span></p>
<h3 id="redux-basic-structure">Redux Basic Structure</h3>
<p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1597494030565/DM52odTuI.png" />
Redux comprises of three main aspects;</p>
<ol>
<li><a class="post-section-overview" href="#action-section">Actions</a>  </li>
<li><a class="post-section-overview" href="#reducer-section">Reducers</a> </li>
<li><a class="post-section-overview" href="#store-section">Store</a> </li>
</ol>
<p><span id="action-section"></span></p>
<h4 id="actions">Actions</h4>
<p>Actions are objects that describes what action you want perform on the state.
A basic action object looks like this.</p>
<pre><code><span class="hljs-keyword">const</span> action ={
         type:<span class="hljs-string">"ACTION_DESCRIPTION"</span>,
         payload: <span class="hljs-keyword">data</span>
     }
</code></pre><p>To make an action reusable, we will have to convert it into a function pseudo-code.</p>
<pre><code><span class="hljs-keyword">const</span> actionDescription = (<span class="hljs-keyword">data</span>)=&gt;{<span class="hljs-keyword">return</span> {
         type:<span class="hljs-string">"ACTION_DESCRIPTION"</span>,
         payload: <span class="hljs-keyword">data</span>
     }}
</code></pre><p>With this, anytime actionDescription(data) is called, it returns an object.
</p>
<p><span id="reducer-section"></span></p>
<h4 id="reducers">Reducers</h4>
<p>Reducers are functions that accept the current state of the application and an action object as arguments to return a new state.
Let's translate that into JavaScript pseudo-code.</p>
<pre><code><span class="hljs-keyword">const</span> reducer =<span class="hljs-function">(<span class="hljs-params">state, action</span>)=&gt;</span> newState;
</code></pre><p>Since a reducer can receive different actions, it makes sense for the reducer to act differently based on the given action.</p>
<pre><code><span class="hljs-keyword">const</span> initialState = {
defaultState
}
<span class="hljs-comment">// use initialstate if no previous state is found</span>
export <span class="hljs-keyword">default</span> (state = initialState, action) =&gt; {
    <span class="hljs-keyword">switch</span> (action.<span class="hljs-keyword">type</span>) {

    <span class="hljs-keyword">case</span> typeName:
        <span class="hljs-keyword">return</span> newState;
    <span class="hljs-keyword">case</span> anotherTypeName:
        <span class="hljs-keyword">return</span> aNewState;

    <span class="hljs-keyword">default</span>:
        <span class="hljs-keyword">return</span> state
    }
}
</code></pre><blockquote>
<p>Note: You are not directly passing state and action to the reducer, the store is in charge of that.
</p>
</blockquote>
<p><span id="store-section"></span></p>
<h4 id="store">Store</h4>
<p>The store is the ultimate source of truth, no one sets, or gets a state from Redux except through it. (I'm bad with references).
What the above gibberish means is that the store handles every transaction between the app, actions, and reducer.
Whatever component that needs the state asks the store.
Whatever component that needs to change the state sends an action to the store.
That is where Redux finally comes into the picture.
Let's see Redux create store.</p>
<pre><code><span class="hljs-keyword">import</span> {createStore} <span class="hljs-keyword">from</span> <span class="hljs-string">'redux'</span>
<span class="hljs-keyword">const</span> store = createStore(reducer)
<span class="hljs-comment">// Create store from reducer</span>
</code></pre><p>We have just created the store, now let's explore some of the methods the store offers</p>
<h5 id="getstate-">getState()</h5>
<p>It is exactly as it sounds, it is used to get the state.</p>
<pre><code>store.getState()
<span class="hljs-comment">// returns current state</span>
</code></pre><h5 id="subscribe-">subscribe(()=&gt;{})</h5>
<p>This where you pass in a function you want to run anytime the state changes.</p>
<pre><code>store.subscribe(<span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span> <span class="hljs-built_in">console</span>.log(store.getState()))
<span class="hljs-comment">// Run console.log(store.getState()) every time the state changes.</span>
</code></pre><blockquote>
<p>When a subscribe function execute, it returns an unsubscribe function which can be accessed by assigning the returned value to a const.</p>
<pre><code><span class="hljs-keyword">const</span> unsubscribe = store.subscribe(<span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span> <span class="hljs-built_in">console</span>.log(store.getState()))
<span class="hljs-comment">// Unsubscribe by calling unsubscribe ()</span>
</code></pre><h5 id="dispatch-actiondescription-">dispatch(actionDescription())</h5>
<p>This is used to call the reducer function and passing the action and current state to it.</p>
<pre><code>store.dispatch(actionDescription(<span class="hljs-keyword">data</span>))
</code></pre><p></p>
</blockquote>
<h3 id="before-we-start-coding">Before we start coding</h3>
<p>Permit me to add one analogy before we move coding proper.
Think of a shop with a strict receptionist who only accept invoice for anything you want to buy and would not permit you to buy anything beyond what the invoice states, the receptionist then sends the invoice to the counter for the product, take the stock after your transaction is completed, then give you what you ordered.</p>
<p>Apply it here, you are the component trying to complete a transaction, the invoice is equivalent to the action, the strict receptionist is the redux store, and the person at the counter is the reducer.</p>
<p>Okay, now let's move to see how everything interact on code.</p>
<p><span id="coding-proper"></span></p>
<h3 id="coding-proper">Coding Proper</h3>
<p></p>
<p>Okay well, let's code!</p>
<p>Direct yourself to the src folder.
Now let's create the component we would work on in the src folder. Let's name it Shop.js. Let's add a functional component.</p>
<pre><code><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Shop</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>

        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    )</span>
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Shop
</code></pre><p>open the App.js file, let's clear the part we don't need and import Shop.js.</p>
<pre><code><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> Shop <span class="hljs-keyword">from</span> <span class="hljs-string">'./Shop'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">Shop</span>/&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  )</span>;
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre><p>Let's move back to the Shop component , and add relevant tags.</p>
<pre><code><span class="hljs-keyword">import</span> React,{useState} <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Shop</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> [number, setNumber] = useState(<span class="hljs-string">''</span>)
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h4</span>&gt;</span>Total Shop item<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'number'</span> <span class="hljs-attr">onChange</span>=<span class="hljs-string">{</span> <span class="hljs-attr">e</span> =&gt;</span> setNumber( e.target.value )} /&gt;
            <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Buy {number} item(s)<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    )</span>
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Shop
</code></pre><p>In the command terminal run </p>
<pre><code><span class="hljs-built_in">npm</span> run start
</code></pre><p>If you used my exact code, you should get 👇</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1597524126882/Gg0ncLUJ_.png" /></p>
<h5 id="time-for-redux-">Time for Redux 🔊</h5>
<p>Create a Redux.js file in the src folder. 
Let's add an action.</p>
<pre><code><span class="hljs-keyword">const</span> buyItem = <span class="hljs-function">(<span class="hljs-params">payload</span>) =&gt;</span> ({
    <span class="hljs-keyword">type</span>: <span class="hljs-string">'BUY_ITEM'</span>,
    payload
})
</code></pre><p>Let's add the reducer function</p>
<pre><code><span class="hljs-keyword">const</span> initialState = {
    shopItem: <span class="hljs-number">50</span>
}
<span class="hljs-keyword">const</span> reducer = <span class="hljs-function">(<span class="hljs-params">state = initialState, action</span>) =&gt;</span> {
    <span class="hljs-keyword">switch</span> (action.type) {

    <span class="hljs-keyword">case</span> <span class="hljs-string">'BUY_ITEM'</span>:
        <span class="hljs-keyword">return</span> { ...state, shopItem: state.shopItem - action.payload }

    <span class="hljs-keyword">default</span>:
        <span class="hljs-keyword">return</span> state
    }
}
</code></pre><p>Since we know we are going to get 'type' and 'payload' from action, let's refactor the code using object destructuring.</p>
<pre><code><span class="hljs-comment">// if no payload is added when buyItem is called use 1 as default</span>
<span class="hljs-keyword">const</span> buyItem = <span class="hljs-function">(<span class="hljs-params">payload=1</span>) =&gt;</span> ({
    <span class="hljs-keyword">type</span>: <span class="hljs-string">'BUY_ITEM'</span>,
    payload
})

<span class="hljs-keyword">const</span> initialState = {
    shopItem: <span class="hljs-number">50</span>
}
<span class="hljs-keyword">const</span> reducer = <span class="hljs-function">(<span class="hljs-params">state = initialState, { <span class="hljs-keyword">type</span>, payload }</span>) =&gt;</span> {
    <span class="hljs-keyword">switch</span> (<span class="hljs-keyword">type</span>) {

    <span class="hljs-keyword">case</span> <span class="hljs-string">'BUY_ITEM'</span>:
        <span class="hljs-keyword">return</span> { ...state, shopItem: state.shopItem - payload }

    <span class="hljs-keyword">default</span>:
        <span class="hljs-keyword">return</span> state
    }
}
</code></pre><p>Now let's setup redux store. First import createStore from redux.</p>
<pre><code><span class="hljs-keyword">import</span> {createStore} <span class="hljs-keyword">from</span> <span class="hljs-string">'redux'</span>
</code></pre><p>Now create store from the reducer.</p>
<pre><code><span class="hljs-keyword">const</span> store = createStore(reducer)
</code></pre><p>Export action and store for react app to utilize.</p>
<pre><code><span class="hljs-keyword">import</span> { createStore } <span class="hljs-keyword">from</span> <span class="hljs-string">'redux'</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> buyItem = <span class="hljs-function">(<span class="hljs-params">payload=1</span>) =&gt;</span> ({
    <span class="hljs-keyword">type</span>: <span class="hljs-string">'BUY_ITEM'</span>,
    payload
})

<span class="hljs-keyword">const</span> initialState = {
    shopItem: <span class="hljs-number">50</span>
}
<span class="hljs-keyword">const</span> reducer= <span class="hljs-function">(<span class="hljs-params">state = initialState, { <span class="hljs-keyword">type</span>, payload }</span>) =&gt;</span> {
    <span class="hljs-keyword">switch</span> (<span class="hljs-keyword">type</span>) {

    <span class="hljs-keyword">case</span> <span class="hljs-string">'BUY_ITEM'</span>:
        <span class="hljs-keyword">return</span> { ...state, shopItem: state.shopItem - payload }

    <span class="hljs-keyword">default</span>:
        <span class="hljs-keyword">return</span> state
    }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> store = createStore(reducer)
</code></pre><p>Now let's move back to App.js and link redux to the JavaScript app.
To link store to the App, we use the Provider component from the react-redux package.</p>
<p>First, import Provider from react-redux and make sure you import the store too</p>
<pre><code><span class="hljs-keyword">import</span> { Provider } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-redux'</span>;
<span class="hljs-keyword">import</span> { store } <span class="hljs-keyword">from</span> <span class="hljs-string">'./Redux'</span>;
</code></pre><p>Second, wrap everything in the 'return' in the Provider component and pass store as a prop.
Your App.js should look like this now.</p>
<pre><code><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> Shop <span class="hljs-keyword">from</span> <span class="hljs-string">'./Shop'</span>;
<span class="hljs-keyword">import</span> { Provider } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-redux'</span>;
<span class="hljs-keyword">import</span> { store } <span class="hljs-keyword">from</span> <span class="hljs-string">'./Redux'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Shop</span>/&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span>
  )</span>;
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre><p>Now that we are done providing the store at the top-most level, Let us move to the Shop.js to access the state provided by the store. </p>
<blockquote>
<p>If you are conversant with React, you will notice that we did not pass store as prop to Shop before leaving App.js. Will we be importing store and Provider in Shop too?
The answer is no. This time, we will 'connect' to the provided state at the top most level. Let's see how to connect.</p>
</blockquote>
<p>There are two main methods of connecting; using <abbr>HOC</abbr> or using hooks. I will be using higher order component method.</p>
<p>To connect, import 'connect' from react-redux.</p>
<pre><code><span class="hljs-keyword">import</span> {connect} from react-redux
</code></pre><p>Things get weird here for most people so let me talk a little about the connect function before we proceed. We won't be adding till the connect is explained.</p>
<h5 id="connect">Connect</h5>
<p>Connect, as the name implies is used to connect a child component to the store provided at the top-most component.</p>
<p><strong>How does it work?</strong></p>
<p>Connect typically requires three arguments to function. check the pseudo-code below.</p>
<pre><code>connect(<span class="hljs-function"><span class="hljs-params">state</span>=&gt;</span>takeRequiredStateAndAddToProp,<span class="hljs-function"><span class="hljs-params">dispatch</span>=&gt;</span> giveComponentPermissionToDispatchAction)(componentToConnectTo)
</code></pre><p>As I tried to show from the pseudo-code above  🌚, the connect is expecting a function in the first two arguments, and expecting a component in the third.</p>
<p>In the first argument, it expects a function that accepts state as an argument and return the part of state it needs so it can connect that part of the state to props. (This mimics store.getState(), then pass the state gotten as an argument). Conventionally, the expected function is named 'mapStateToProps'.</p>
<blockquote>
<p>You can name the function anything but for consistency name it 'mapStateToProps'.</p>
</blockquote>
<p>A typical mapStateToProps functions looks like this pseudo-code.</p>
<pre><code><span class="hljs-keyword">const</span> mapStateToProps = <span class="hljs-function">(<span class="hljs-params">state</span>) =&gt;</span> ({
    aNeededstate: state.aNeededstate,
    anotherState: state.anotherState
})
</code></pre><p>In the second argument, it expects a function that accepts dispatch as an argument and return an object of functions that can dispatch actions it needs so it can connect the object to props. (This mimics dispatch = state.dispatch() and then  pass dispatch as an argument). Conventionally, the expected function is named 'mapDispatchToProps'.</p>
<blockquote>
<p>You can name the function anything but for consistency name it 'mapDispatchToProps'.</p>
</blockquote>
<p>A typical mapDispatchToProps functions looks like this pseudo-code.</p>
<pre><code><span class="hljs-comment">// import necessary actions because you will dispatch it here</span>
<span class="hljs-keyword">const</span> mapDispatchToProps = <span class="hljs-function">(<span class="hljs-params">dispatch</span>) =&gt;</span> ({
    actionOne: <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> dispatch(actionOne),
    actionTwo: <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> dispatch(actionTwo)
})
</code></pre><p>Connect will now look very close to like this.</p>
<pre><code><span class="hljs-keyword">connect</span>(mapStateToProps,mapDispatchToProps)(component)
</code></pre><p>If you only want to dispatch action but you don't want the component to access state or it does not need the redux state, you don't need to declare mapStateToProps, you can just add null in its place.</p>
<pre><code>connect( <span class="hljs-literal">null</span>, mapDispatchToProps )(component)
</code></pre><p>Also, if you only need to access state, you can pass only mapStateToProps.</p>
<pre><code><span class="hljs-keyword">connect</span>(mapStateToProps)(component)
</code></pre><p>Now, let's shift our focus back to Shop.js.</p>
<p>Import the action(s) needed. In this case, buyItem.</p>
<pre><code><span class="hljs-keyword">import</span> { buyItem } <span class="hljs-keyword">from</span> <span class="hljs-string">'./Redux'</span>
</code></pre><p>After the Shop function, let's declare the mapStateToProps and mapDispatchToProps functions. </p>
<pre><code><span class="hljs-comment">// since the shopItem is the only content of the state, let's spread the state</span>
<span class="hljs-keyword">const</span> mapStateToProps = <span class="hljs-function">(<span class="hljs-params">state</span>) =&gt;</span> ({
    ...state
})
<span class="hljs-comment">// You can also write</span>
<span class="hljs-comment">// const mapStateToProps = state =&gt;state</span>
<span class="hljs-comment">// or</span>
<span class="hljs-comment">// const mapStateToProps = (state) =&gt; ({</span>
<span class="hljs-comment">//    shopItem: state.shopItem</span>
<span class="hljs-comment">// })</span>
</code></pre><pre><code><span class="hljs-keyword">const</span> mapDispatchToProps = <span class="hljs-function"><span class="hljs-params">dispatch</span> =&gt;</span> ({
    buyItem: <span class="hljs-function">(<span class="hljs-params"><span class="hljs-built_in">number</span></span>)=&gt;</span>dispatch(buyItem(<span class="hljs-built_in">number</span>))
})
</code></pre><p>Let's connect the component to the store. Instead of exporting the component directly we will wrap the connect around it before exporting.</p>
<pre><code><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect( mapStateToProps, mapDispatchToProps )(Shop)
</code></pre><p>The code should look like this.</p>
<pre><code><span class="hljs-keyword">import</span> React,{useState} <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-redux'</span>
<span class="hljs-keyword">import</span> { buyItem } <span class="hljs-keyword">from</span> <span class="hljs-string">'./Redux'</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Shop</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> [number, setNumber] = useState(<span class="hljs-string">''</span>)
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h4</span>&gt;</span>Total Shop item<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'number'</span> <span class="hljs-attr">onChange</span>=<span class="hljs-string">{e=</span>&gt;</span>setNumber(e.target.value)} /&gt;
            <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Buy {number} item(s)<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    )</span>
}

<span class="hljs-keyword">const</span> mapStateToProps = state =&gt; state

<span class="hljs-keyword">const</span> mapDispatchToProps = dispatch =&gt; ({
    buyItem: (number)=&gt;dispatch(buyItem(number))
})


<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect( mapStateToProps ,mapDispatchToProps )(Shop)
</code></pre><p>With this, redux state and dispatch have been "magically" added to props. (maybe 'map' at the start of the function name means "Magically Add to Props" #JustBants 😆.</p>
<p>To access state, just use {props.stateContent}. We want to access shopItem in the state and display in the h4 tag </p>
<pre><code><span class="hljs-comment">// Add props as an argument in the Shop function</span>
&lt;h4&gt;Total Shop item: {props.shopItem} &lt;/h4&gt;
</code></pre><p>To access dispatch, just use {props.dispatchName}. We want to access buyItem in the dispatch and use in the button tag</p>
<pre><code>&lt;input <span class="hljs-keyword">type</span>=<span class="hljs-string">'number'</span> onChange={<span class="hljs-function"><span class="hljs-params">e</span>=&gt;</span>setNumber(e.target.value)} value={<span class="hljs-built_in">number</span>}/&gt;
            &lt;button onClick={<span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span>{props.buyItem(<span class="hljs-built_in">number</span>); setNumber(<span class="hljs-string">''</span>)}}&gt;Buy {<span class="hljs-built_in">number</span>} item(s)&lt;<span class="hljs-regexp">/button&gt;</span>
</code></pre><p>The code now look like this.</p>
<pre><code><span class="hljs-keyword">import</span> React,{useState} <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-redux'</span>
<span class="hljs-keyword">import</span> { buyItem } <span class="hljs-keyword">from</span> <span class="hljs-string">'./Redux'</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Shop</span>(<span class="hljs-params">props</span>) </span>{
    <span class="hljs-keyword">const</span> [number, setNumber] = useState(<span class="hljs-string">''</span>)
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h4</span>&gt;</span>Total Shop item: {props.shopItem}<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'number'</span> <span class="hljs-attr">onChange</span>=<span class="hljs-string">{e=</span>&gt;</span>setNumber(e.target.value)} value={number}/&gt;
            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()=</span>&gt;</span>{props.buyItem(number); setNumber('')}}&gt;Buy {number} item(s)<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    )</span>
}

<span class="hljs-keyword">const</span> mapStateToProps = state =&gt; state

<span class="hljs-keyword">const</span> mapDispatchToProps = dispatch =&gt; ({
    buyItem: (number)=&gt;dispatch(buyItem(number))
})

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect( mapStateToProps ,mapDispatchToProps )(Shop)
</code></pre><p><span id="summary-section"></span></p>
<h2 id="summary">Summary</h2>
<p>
Redux is a state container that can be used to handle states in JavaScript application.</p>
<p>Redux is used in React as one of the solutions to unnecessary state passing from parents to components.</p>
<p>Redux does not allow the React app to directly change the state, it has to dispatch an action to the reducer through store.</p>
<p>To bridge React and Redux, react-redux package was introduced.
We used two of what react-redux provide in this article (Provider and connect).</p>
<p>The Provider makes the Redux store available for the React app.</p>
<p>The connect grants access to the state and make dispatching action possible.</p>
<p>That's all for now.</p>
]]></content:encoded></item></channel></rss>