<?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: Najmul Ovi</title>
    <description>The latest articles on Forem by Najmul Ovi (@mdnajmul).</description>
    <link>https://forem.com/mdnajmul</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%2F821094%2F6dc55345-2045-44f0-8025-1b8b1a1010bd.jpeg</url>
      <title>Forem: Najmul Ovi</title>
      <link>https://forem.com/mdnajmul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mdnajmul"/>
    <language>en</language>
    <item>
      <title>REDUX TOOLKIT FUNDAMENTAL</title>
      <dc:creator>Najmul Ovi</dc:creator>
      <pubDate>Fri, 04 Mar 2022 12:08:55 +0000</pubDate>
      <link>https://forem.com/mdnajmul/redux-toolkit-fundamental-545i</link>
      <guid>https://forem.com/mdnajmul/redux-toolkit-fundamental-545i</guid>
      <description>&lt;p&gt;&lt;strong&gt;Why Redux Toolkit?&lt;/strong&gt;&lt;br&gt;
We know that Redux was a popular option for state management. It makes states predictable. Reducers are pure functions that means the same state and actions are passed will always result in the same output.&lt;br&gt;
Redux also easily maintainable, scalable, efficient testing, easy debugging and better performance that Redux brings to build applications.&lt;/p&gt;

&lt;p&gt;However, this flexible and high-level state management library comes with a few challenges:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Too much code to configure store.&lt;/li&gt;
&lt;li&gt;Too much boilerplate code.&lt;/li&gt;
&lt;li&gt;Too many packages needed to install to build scalable apps.&lt;/li&gt;
&lt;li&gt;Writing actions and reducers becomes more complex on large applications.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To overcome these challenges, The Redux team came up with Redux Toolkit. It's main purpose is to speed up Redux development by including Redux Core with the packages that they think are essential to build a Redux app. It also makes our code and folder structure more understandable and organized.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How We Started With Redux Toolkit?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Packages Install&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @reduxjs/toolkit react-redux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app my-app --template redux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Initialize &amp;amp; Create Store&lt;/strong&gt;&lt;br&gt;
We can create store.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from '@reduxjs/toolkit'

export default configureStore({
  reducer: {} //add reducers here
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Provide Store in React app&lt;/strong&gt;&lt;br&gt;
In our index.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import store from './store'
import { Provider } from 'react-redux'

ReactDOM.render(
  &amp;lt;Provider store={store}&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/Provider&amp;gt;,
  document.getElementById('root')
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Reducers and Actions&lt;/strong&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 { createSlice } from '@reduxjs/toolkit'

export const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    value: 0
  },
  reducers: {
    increase: state =&amp;gt; {
      state.value += 1
    },
    decrease: state =&amp;gt; {
      state.value -= 1
    }
  }
})

// each case under reducers becomes an action
export const { increase, decrease } = counterSlice.actions

export default counterSlice.reducer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5: Import Reducer to Store&lt;/strong&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 { configureStore } from '@reduxjs/toolkit'
import counterReducer from '.counterSlice' //import our reducer from step 4

export default configureStore({
  reducer: {
    counter: counterReducer //add our reducer from step 4
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 6: Dispatch Actions from UI&lt;/strong&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 { useSelector, useDispatch } from 'react-redux'
import { decrease, increase } from './counterSlice'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>redux</category>
      <category>react</category>
    </item>
    <item>
      <title>REDUX FUNDAMENTAL</title>
      <dc:creator>Najmul Ovi</dc:creator>
      <pubDate>Thu, 24 Feb 2022 12:13:59 +0000</pubDate>
      <link>https://forem.com/mdnajmul/redux-fundamental-202c</link>
      <guid>https://forem.com/mdnajmul/redux-fundamental-202c</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Redux?&lt;/strong&gt;&lt;br&gt;
Redux is a state management library for JavaScript applications. If we want to share state data from one component to another, we need passes from parent component to child component in the form of props. Using Redux we don't need props drilling to share data from one component to another because Redux is considered as a central store. Context APIs solve similar problems as Redux in this age, but Redux is better option for large scale applications where we need handle many state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwyt2m8stzg3sqccjt67k.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwyt2m8stzg3sqccjt67k.png" alt="Redux Store"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Image Source:&lt;/em&gt; &lt;a href="https://blog.codecentric.de/" rel="noopener noreferrer"&gt;codecentric&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Three Core Principle of Redux&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Store:&lt;/strong&gt; It provides a universal data storage. We can create Redux store using createStore(). We must pass reducer as its first parameter. It takes an optional second parameter as 'initialState' and an optional 'middleware' which allows us to pass multiple middlewares like 'thunk', 'redux-logger', etc.&lt;br&gt;
&lt;strong&gt;2. Actions:&lt;/strong&gt; Actions are kind of events and they are just objects. When an event is trigged inside the application, then an action created which give instructions to update the state.&lt;br&gt;
&lt;strong&gt;3. Reducers:&lt;/strong&gt; When the action is dispatched and sent to the store, then the store holds the application state and updates the state by using the reducer function.&lt;br&gt;
&lt;strong&gt;Finally,&lt;/strong&gt; Update states are sent to UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of Redux&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Predictable state changes&lt;/li&gt;
&lt;li&gt;Centralized state&lt;/li&gt;
&lt;li&gt;Easy debugging&lt;/li&gt;
&lt;li&gt;Preserve page state&lt;/li&gt;
&lt;li&gt;Implement Undo/Redo features&lt;/li&gt;
&lt;li&gt;Large &amp;amp; growing Ecosystem of add-ons&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages of Redux&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Complexity&lt;/li&gt;
&lt;li&gt;Need to write some boiler plate code to get things done&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When not to use Redux&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tight budget&lt;/li&gt;
&lt;li&gt;Small to medium size applications&lt;/li&gt;
&lt;li&gt;Simple UI/data flow&lt;/li&gt;
&lt;li&gt;Static data&lt;/li&gt;
&lt;/ul&gt;

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