<?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: Mnabawy</title>
    <description>The latest articles on DEV Community by Mnabawy (@mnabawy).</description>
    <link>https://dev.to/mnabawy</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%2F488747%2F6dd37a35-bc6d-4d51-816b-5f5677d4a4d7.jpeg</url>
      <title>DEV Community: Mnabawy</title>
      <link>https://dev.to/mnabawy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mnabawy"/>
    <language>en</language>
    <item>
      <title>redux toolkit</title>
      <dc:creator>Mnabawy</dc:creator>
      <pubDate>Wed, 23 Mar 2022 20:04:57 +0000</pubDate>
      <link>https://dev.to/mnabawy/redux-toolkit-378f</link>
      <guid>https://dev.to/mnabawy/redux-toolkit-378f</guid>
      <description>&lt;p&gt;in this post we will know how usefull redux toolkit is and what it prings to the table &lt;/p&gt;

&lt;p&gt;-first lets configure redux store for the counter app the old way &lt;br&gt;
-then lets fire some actions &lt;br&gt;
-then i will write the same code with the toolkit &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;here is my redux setup with the old way&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 { createStore } from "redux";

const initialState = {
  counter: 0,
};

// actions
export const INCREMENT = "INCREMENT";
export const DECREMENT = "DECREMENT";
export const RESET = "RESET";

// counter reducer
const counterReducer = (state = initialState, action) =&amp;gt; {
  if (action.type === INCREMENT) {
    return {
      counter: state.counter + 1,
    };
  }
  if (action.type === DECREMENT) {
    return {
      counter: state.counter - 1,
    };
  }
  if (action.type === RESET) {
    return {
      counter: 0,
    };
  }

  return state;
};

// ccounter store
const store = createStore(counterReducer);
export default store;

//here we fire diffrent actions 
// increment
dispatch({type: INCREMENT})

// decrement
dispatch({type: DECREMENT})

// reset
dispatch({type: RESET})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;and here is the same approach with redux toolkit&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, createSlice } from "@reduxjs/toolkit";
// initial state
const initialState = {
  counter: 0,
};

const counterSlice = createSlice({
  name: "counter",
  initialState: initialState,
  // here we replaced the reducer with this helper function which gives us ability to mutate the state directly 
  reducers: {
    increment(state) {
      state.counter++;
    },
    decrement(state) {
      state.counter--;
    },
    reset(state) {
      state.counter = 0;
    },
    increase(state) {
      state.counter = state.counter + action.payload;
    },
  },
});

// actions to be used from inside the component later to make changes to our state 
export const counterActions = counterSlice.actions;

const store = configureStore({
  reducer: counterSlice.reducer,
});

export default store;

//increment
counterActions.increment()

//decrement
counterActions.decrement()

//reset
counterActions.reset()

// and if you asking of course you can pass data (payload) 
like this 

for example as a parameter becase we still have access to the action payload
counterActions.increase(5)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;quik recap what we has accomplished so far &lt;br&gt;
-we made a simple counter with react and redux with two ways &lt;br&gt;
-the first way we used the old way of redux and it's a little confusing and complex to configure at first&lt;br&gt;
-so here comes redux toolkit to solve this problm &lt;br&gt;
-the mean reason for using toolkit is to simplify redux configuration &lt;br&gt;
-here i am not talking about the pros and cons of using both methods a i prefered to just explain the diffrent in a practical way&lt;/p&gt;

&lt;p&gt;hope you enjoy it &lt;br&gt;
;) &lt;/p&gt;

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