<?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: Mamun Ligzer</title>
    <description>The latest articles on Forem by Mamun Ligzer (@ligzer_dev).</description>
    <link>https://forem.com/ligzer_dev</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%2F800631%2F7fc976ca-edd4-4e27-84af-8c7b57c0112c.png</url>
      <title>Forem: Mamun Ligzer</title>
      <link>https://forem.com/ligzer_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ligzer_dev"/>
    <language>en</language>
    <item>
      <title>Create contextual modal navigation with React Router V6.</title>
      <dc:creator>Mamun Ligzer</dc:creator>
      <pubDate>Fri, 15 Apr 2022 16:43:57 +0000</pubDate>
      <link>https://forem.com/ligzer_dev/create-contextual-modal-navigation-with-react-router-v6-28k2</link>
      <guid>https://forem.com/ligzer_dev/create-contextual-modal-navigation-with-react-router-v6-28k2</guid>
      <description>&lt;p&gt;I'm currently (April 2022) creating a side-project using ReactJs. I have taken inspiration from various existing popular websites like Twitter, Facebook, Trello, etc. I was trying to create an edit-profile UI like Twitter. When you click the Edit Profile button, a pop-up window opens and the URL changes. But the previous page remains in the background. After closing the popup, it goes back to the previous page.&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1649944317683%2F4rbTMUAoj.gif" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1649944317683%2F4rbTMUAoj.gif" width="600" height="318"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;I had no idea how to do it. I searched it on Google, but I found some old tutorials. Note: I am using React Router V6. Finally, I did it. Now, I will show you how I did it.&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1649945258156%2FXQHc834qX.gif" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1649945258156%2FXQHc834qX.gif" width="600" height="318"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Start
&lt;/h2&gt;

&lt;p&gt;First thing first, create a react app and install react-router-dom.&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
cd my-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react-router-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have deleted all of the test files. You can keep them if you want. Create a "Components" folder. Here we will put our homepage and model. Create two files named &lt;code&gt;Modal.js&lt;/code&gt; and &lt;code&gt;Main.js&lt;/code&gt; inside the "Components" folder. &lt;code&gt;Main.js&lt;/code&gt; is our homepage.&lt;/p&gt;

&lt;h3&gt;
  
  
  Main.js
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Link, useLocation } from "react-router-dom";

export const Main = () =&amp;gt; {
  const location = useLocation();
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;Create contextual modal navigation&amp;lt;/h2&amp;gt;
      &amp;lt;Link to="/modal" state={{ background: location }}&amp;gt;
        Open Modal
      &amp;lt;/Link&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Main.js&lt;/code&gt; is a react-arrow-functional-component. We have two elements here &lt;code&gt;&amp;lt;h2/&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Link /&amp;gt;&lt;/code&gt;. Note: The &lt;code&gt;&amp;lt;Link /&amp;gt;&lt;/code&gt; element contains an additional state attribute. It contains an object. We will pass background as key and location as value. We will use this object in the future.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modal.js
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useNavigate } from "react-router-dom";

export const Modal = () =&amp;gt; {
  const navigate = useNavigate();
  return (
    &amp;lt;div className="modalDiv"&amp;gt;
      &amp;lt;div className="modal"&amp;gt;
        &amp;lt;h3&amp;gt;Modal&amp;lt;/h3&amp;gt;
        &amp;lt;button onClick={() =&amp;gt; navigate(-1)}&amp;gt;Close&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  App.css
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.App {
  text-align: center;
}

.modalDiv {
  width: 100vw;
  height: 100vh;
  position: absolute;
  top: 0;
  background-color: rgba(91, 112, 131, 0.4);
  display: flex;
  justify-content: center;
  align-items: center;
}
.modal {
  width: 350px;
  height: 200px;
  background-color: white;
  border-radius: 5px;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Index.js
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import { BrowserRouter as Router } from "react-router-dom";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  &amp;lt;Router&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/Router&amp;gt;
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have wrapped &lt;code&gt;&amp;lt;App /&amp;gt;&lt;/code&gt; with &lt;code&gt;&amp;lt;Router /&amp;gt;&lt;/code&gt; inside the &lt;code&gt;Index.js&lt;/code&gt; file instead of placing it on the &lt;code&gt;App.js&lt;/code&gt; file. This is because we will use the useLocation hook used by the React Router in the App.js file. We're not allowed to place any hook used by the React router outside of &lt;code&gt;&amp;lt;Router /&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  App.js
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./App.css";
import { Route, Routes, useLocation } from "react-router-dom";
import { Main } from "./components/Main";
import { Modal } from "./components/Modal";
function App() {
  const location = useLocation();
  const background = location.state &amp;amp;&amp;amp; location.state.background;

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;Routes location={background || location}&amp;gt;
        &amp;lt;Route path="/" element={&amp;lt;Main /&amp;gt;}&amp;gt;
          &amp;lt;Route path="modal" element={&amp;lt;Modal /&amp;gt;} /&amp;gt;
        &amp;lt;/Route&amp;gt;
      &amp;lt;/Routes&amp;gt;
      {background &amp;amp;&amp;amp; (
        &amp;lt;Routes&amp;gt;
          &amp;lt;Route path="modal" element={&amp;lt;Modal /&amp;gt;} /&amp;gt;
        &amp;lt;/Routes&amp;gt;
      )}
    &amp;lt;/div&amp;gt;
  );
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we click on the open-modal to open the modal, we do not want to show only the modal with a blank page in the background. We want to show the modal on top of the previous page.&lt;/p&gt;

&lt;p&gt;So we need to pass the previous location object to &lt;code&gt;&amp;lt;Routes /&amp;gt;&lt;/code&gt; instead of using the current location object by default. Thus &lt;code&gt;&amp;lt;Routes /&amp;gt;&lt;/code&gt; thinks we are on the same page (previous location). For example, we are on the home page &lt;code&gt;http://localhost:3000/&lt;/code&gt;. When we click on the link to open the modal, the position changes to &lt;code&gt;https://localhost:3000/modal&lt;/code&gt; but &lt;code&gt;&amp;lt;Routes /&amp;gt;&lt;/code&gt; thinks the position has never changed.&lt;/p&gt;

&lt;p&gt;Remember? We passed a state attribute in the &lt;code&gt;main.js&lt;/code&gt; file, which had a background object. If there is a background object, upon clicking the link to open the modal, the model will be conditionally shown by the second &lt;code&gt;&amp;lt;Routes /&amp;gt;&lt;/code&gt; container, and the homepage will be shown as background by the first &lt;code&gt;&amp;lt;Routes /&amp;gt;&lt;/code&gt; container.&lt;/p&gt;

&lt;p&gt;But when you directly visit the modal page, we will only see the homepage even though we have added the modal route in the first  container. You can show the model or any other component for the /model path by simply adding the &lt;code&gt;&amp;lt;Outlet /&amp;gt;&lt;/code&gt; element to the &lt;code&gt;Main.js&lt;/code&gt; file. For this demo, we will show the model.&lt;/p&gt;

&lt;h3&gt;
  
  
  Main.js
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Add &lt;code&gt;&amp;lt;Outlet/&amp;gt;&lt;/code&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Link, Outlet, useLocation } from "react-router-dom";

export const Main = () =&amp;gt; {
  const location = useLocation();
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;Create contextual modal navigation&amp;lt;/h2&amp;gt;
      &amp;lt;Link to="modal" state={{ background: location }}&amp;gt;
        Open Modal
      &amp;lt;/Link&amp;gt;
      // Here is the &amp;lt;Outlet/&amp;gt;
      &amp;lt;Outlet /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1650027952727%2F5y3_wxYoW.gif" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1650027952727%2F5y3_wxYoW.gif" width="600" height="315"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;I hope, I was able to explain this. If you have any questions or suggestions about this blog, reach out to me via &lt;a href="https://www.twitter.com/ligzer_dev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://dreamy-macaron-0c86da.netlify.app" rel="noopener noreferrer"&gt;Live Demo.&lt;/a&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://github.com/devmdmamun/contextual-modal-navigation-with-react-router" rel="noopener noreferrer"&gt;Source code on GitHub&lt;/a&gt;
&lt;/h4&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://reactrouter.com/docs/en/v6/examples/modal" rel="noopener noreferrer"&gt;Official React Router modal example&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.logrocket.com/building-a-modal-module-for-react-with-react-router/" rel="noopener noreferrer"&gt;Building a modal module for React with React-Router V5 by Doğacan Bilgili&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Redux and Redux toolkit - simply explained.</title>
      <dc:creator>Mamun Ligzer</dc:creator>
      <pubDate>Thu, 10 Feb 2022 18:50:43 +0000</pubDate>
      <link>https://forem.com/ligzer_dev/redux-and-redux-toolkit-simply-explained-1b1b</link>
      <guid>https://forem.com/ligzer_dev/redux-and-redux-toolkit-simply-explained-1b1b</guid>
      <description>&lt;h2&gt;
  
  
  Introduction :
&lt;/h2&gt;

&lt;p&gt;Since this article is about redux, I expect you know about ReactJs. If you don't know about ReactJs, learn ReactJs first.&lt;/p&gt;

&lt;p&gt;Redux is a state management library for javascript. It was mainly created to help manage the state of React applications. But you can use redux with other javascript libraries or frameworks such as &lt;a href="https://angularjs.org/" rel="noopener noreferrer"&gt;ANGULAR&lt;/a&gt; or &lt;a href="https://vuejs.org/" rel="noopener noreferrer"&gt;VUE&lt;/a&gt;. I personally use Redux to manage the state of large react applications. I don’t recommend using redux for simple applications because react hooks do that job perfectly. &lt;/p&gt;

&lt;p&gt;For me when applications get bigger, managing state using react hooks seems pretty messy. That’s when I consider using Redux. If you’re creating a project that going to scale you should use Redux. By using the redux toolkit, it's very easy to do redux development. Redux toolkit is an official, opinionated, batteries-included toolset for efficient Redux development. It does a lot of the work in the background. Managing state is easier using the redux toolkit.&lt;/p&gt;

&lt;p&gt;By creating a counting app, I will show you how to use the Redux and Redux toolkit. If you previously worked with (context and reducer) hooks in React Js these learning steps will be as easy as drinking coffee. Because I'm not going to explain those basic terms.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Create a new react app :
&lt;/h3&gt;



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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Install redux :
&lt;/h3&gt;

&lt;p&gt;Redux alone doesn't care about your react app. That's why we have to install react-redux. And also the redux-thunk middleware to work with asynchronous actions.&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 redux react-redux redux-thunk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installing, you will see those dependencies in your package.json file. Then create a folder in your src directory to store all of your state-related {logic/codes}. I will name this folder state. You can name it as you like.&lt;/p&gt;

&lt;p&gt;After completing all these setups, the first thing we are going to do is create a reducer in Redux. Now we will create another folder called Reducers inside the State folder in which we will save all of our Reducers.&lt;/p&gt;

&lt;p&gt;Now create a JavaScript file called CountingReducer.js inside the Reducer folder. You may have multiple reducer files for your application. But for this project, we will use only one reducer file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reducer :
&lt;/h3&gt;

&lt;p&gt;The reducer is a simple javascript function that returns states based on action passed to the reducer. Reducer in Redux takes two parameters, the first one is the initial state and the second one is an action. To return state based on actions we will use a switch statement. You can also use if-else, but it's not recommended.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//created the function with parameters.
const reducer = (state = 0, action) =&amp;gt; {
  // reads the action and returns a state.
  switch (action.type) {
    case "INCREMENT":
      return state + action.payload;
    case "DECREMENT":
      return state - action.payload;
    default:
      return state;
  }
};

//exporting the function.
export default reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have created our reducer 🎉. The next step is to combine our reducers. We don't have multiple reducers in this project, but we still have to combine reducers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Combine reducers :
&lt;/h3&gt;

&lt;p&gt;To combine Reducers, we need to create another file inside the Reducers folder. I will name it combReducers.js.&lt;/p&gt;

&lt;p&gt;First, import combineReducers from redux. After that, import reducers from reducer files. Next, pass a key-value pair to the combineReducers function for each reducer. &lt;/p&gt;

&lt;p&gt;For example, we would call the countingReducer "count". And the value of the count will be the value returned by the countingReducer.&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
import { combineReducers } from "redux";
// import countingReducer for countingReducer.js
import countingReducer from "./countingReducer";
const reducers = combineReducers({
  // key:     value
  count: countingReducer,
});
// exportin reducers
export default reducers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Store :
&lt;/h3&gt;

&lt;p&gt;After combining the reducers. We will create a js file named store.js in the state folder. In store.js we have to import createStore from redux and reducers from combReducers.js.&lt;/p&gt;

&lt;p&gt;CreateStore usually takes two parameters, the first one is reducers and the second one is a default state. Lastly, to work with async actions, we have to pass thunk middleware as the third parameter. To do that, we have to import applyMiddleware from redux and thunk middleware from redux-thunk. Then we will pass the applyMiddleware(thunk) as the third parameter.&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 and applyMiddleware
import { createStore, applyMiddleware } from "redux";
// import reducers
import reducers from "./reducers/combReducers";
// import thunk middleware
import thunk from "redux-thunk";

// export the store.
export const store = createStore(reducers, {}, applyMiddleware(thunk));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Provider :
&lt;/h3&gt;

&lt;p&gt;To access our store from our react application, we need to wrap our whole application with a provider from react-redux. To do that, at the index.js file in the src folder, we have to import the provider from react-redux and the store from the store.js file. Then wrap the app component with the provider. Then specify the store to the provider.&lt;br&gt;
&lt;/p&gt;

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

ReactDOM.render(
  &amp;lt;React.StrictMode&amp;gt;
    {
      // wrap the app with the provider.
    }
    &amp;lt;Provider store={store}&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/Provider&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;,
  document.getElementById("root")
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can access the state from anywhere in our react project. To access the store, at the App.js file in the src folder, we have to import useSelector from react-redux. Then we will create a variable called count and assign it to the useSelector. useSelector is going to take a function as a parameter that will return our state.&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.
import { useSelector } from "react-redux";
// getting the state.
const count = useSelector((state) =&amp;gt; state.count);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to see the state you can log the count variable to the console.&lt;/p&gt;

&lt;h3&gt;
  
  
  Action Creators :
&lt;/h3&gt;

&lt;p&gt;Now, In order to increment or decrement, we have to create actions. And we are going to do that with action creators. To do that, inside the state folder we will create another folder called action-creators. This is the folder where all of our action creator files will be stored. Inside this folder, we are going to create a Js file named index.js. Action creator is a function that dispatches an action. Now we are going to create two action creators inside the index.js file, one for increment and another one for decrement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const increment = (ammount) =&amp;gt; {
  return (dispatch) =&amp;gt; {
    dispatch({
      type: "INCREMENT",
      payload: ammount,
    });
  };
};

export const decrement = (ammount) =&amp;gt; {
  return (dispatch) =&amp;gt; {
    dispatch({
      type: "DECREMENT",
      payload: ammount,
    });
  };
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we will export all of the action creators from a central file as actionCreators. For that, create a js file called index.js inside the state folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export * as actionCreators from "./action-creators/index";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can use actionCreators to increment and decrement from our App.js file. &lt;/p&gt;

&lt;p&gt;To use actionCreators, we will import actionCreators, useDispatch from react-redux, and bindActionCreators from redux. First, we are going to assign useDispatch to a variable. Then we will bind the actionCreatros using bindActionCreators and pass the dispatch into it. After that, we will destructure increment and decrement from bindActionCreators. Finally, we will show the count. And by using destructured actions in two buttons, we will be able to increment and decrement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./App.css";
// import useSelector &amp;amp; useDispatch from react-redux
import { useSelector, useDispatch } from "react-redux";
// import bindActionCreators from redux
import { bindActionCreators } from "redux";
// import actionCreators
import { actionCreators } from "./state/index";

function App() {
  const count = useSelector((state) =&amp;gt; state.count);
  // assigned useDispatch to dispatch variable.
  const dispatch = useDispatch();

  // destructuring increment and decrement actions
  const { increment, decrement } = bindActionCreators(actionCreators, dispatch);

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;h2&amp;gt;{count}&amp;lt;/h2&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; increment(100)}&amp;gt;increment&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; decrement(100)}&amp;gt;decrement&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Final product :
&lt;/h3&gt;

&lt;p&gt;If you followed along with me, you should have a counting app like this one.&lt;br&gt;
&lt;a href="https://media2.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%2Fhfl4fjkg7cpagw65fqij.gif" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhfl4fjkg7cpagw65fqij.gif" alt="redux.gif" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How it works :
&lt;/h3&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2zvn1hwxegodl3ls1uy.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2zvn1hwxegodl3ls1uy.png" alt="@DevMdMamun.png" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you click the increment or decrement button from UI, it fires a function telling the action creator to create an action. That action then passed into the reducer. Reducer dispatches the action and returns a state. That state is then stored in the store. And our application reads the new state from the store and updates the UI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/devmdmamun/basic-redux" rel="noopener noreferrer"&gt;You will find this project in my GitHub repository.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux Toolkit :
&lt;/h2&gt;

&lt;p&gt;If you wish to use redux in your application, you should use the redux toolkit. This simplifies all the processes described above.&lt;/p&gt;

&lt;p&gt;I also wanted to show, how this counting application can be created using the redux toolkit. But it is well shown in the &lt;a href="https://redux.js.org/tutorials/quick-start" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;. So I'm not going to repeat. If you're interested visit &lt;a href="https://redux.js.org/tutorials/quick-start" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://redux.js.org/tutorials/quick-start" rel="noopener noreferrer"&gt;Official Redux documentation.&lt;/a&gt; &lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=9jULHSe41ls" rel="noopener noreferrer"&gt;Youtube video by Laith Harb.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;I hope, this blog was helpful for you. If you have any recommendations, questions, or suggestions about this blog, please reach out to me on &lt;a href="https://twitter.com/devmdmamun" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or comment on this blog.&lt;/p&gt;

&lt;p&gt;This blog is originally posted on &lt;a href="https://blog.devmdmamun.com" rel="noopener noreferrer"&gt;DevMdMamun's blog&lt;/a&gt;&lt;/p&gt;

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