Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Angular

    How to Upload Images to a Cloud Storage(Imgur) in an Angular Application

    JavaScript

    React Lesson 14: Redux Thunk Deep Dive

    Beginners

    NextJS Tutorial: Getting Started with NextJS

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / React / React Lessons / React Lesson 11. Pt.2: Redux Middlewares
    JavaScript

    React Lesson 11. Pt.2: Redux Middlewares

    Mohammad Shad MirzaBy Mohammad Shad MirzaMarch 23, 2020Updated:December 6, 2024No Comments4 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    React Lesson 11. Pt.2: Redux Middlewares
    React Lessons. Lesson 11. Pt.2. Redux Middlewares
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    React Lessons. Lesson 11. Pt.2. Redux Middlewares
    React Lessons. Lesson 11. Pt.2. Redux Middlewares

    In this lesson, we will learn about middlewares in Redux. We will understand what are they, how we can create one and how to use it in our app.

    What is middleware?

    Middleware provides a way to interact with actions that have been dispatched to the store before they reach the store’s reducer. Examples of different uses for middleware include logging actions, reporting errors, making asynchronous requests, and dispatching new actions.

    We talked about side-effects in the previous lesson and the simplest example of side-effects is logging.

    We will create a logger middleware that logs before, after, and when an action is dispatched. Let’s get started:

    Step 1: Create a middleware

    Create a folder middlewares with a file logger.js inside:

    export default store => next => action => {
      console.log("---", "before: ", store.getState());
      console.log("---", "dispatching", action);
      next(action);
      console.log("---", "after", store.getState());
    };

    A middleware is a function that receives a store and returns a new function. This new function receives next (the function for further control delivery), which returns the function accepting action that does something.

    This scheme enables you to have access to the current value of your store, to next, and to action. Our store will show immutable.js structures. It is happening with the help of our recordsFromArray from reducer/utils.js. And what is more important is that the state of our store can change throughout the lifecycle of this middleware. We will see it right here, in our logger.js.

    First, we will do dispatch of the “before” state and then, we’ll call next – the delivery of control further. Generally, the whole chain of middleware functions exists, and it goes from one to another, which means we’ve handled our action in this middleware. Then, we deliver management using next to the next one, and whenever middlewares are over, it gets to reducers for being handled there and goes to store. Once we are done with the dispatch of this action, we return to our middleware and can get the current state of the store after it has been handled in reducers.

    Read More:  Contentful+Gatsby = Smarter content management

    Step 2: Connect middleware to store

    Now we need to connect our middleware to store. Change store/index.js:

    import { createStore, applyMiddleware, compose } from "redux";
    import reducer from "../reducers";
    
    //import logger
    import logger from "../middlewares/logger";
    
    //define enhancer
    const enhancer = compose(
      applyMiddleware(logger),
      window.devToolsExtension ? window.devToolsExtension() : f => f
    );
    
    //update createStore
    const store = createStore(reducer, {}, enhancer);
    
    export default store;

    createStore can accept three arguments:

    1. reducer
    2. [preloadedState]
    3. [enhancer]

    We have to focus on enhancer for now. You may optionally specify it to enhance the store with third-party capabilities such as middleware, time travel, persistence, etc. The only store enhancer that ships with Redux is applyMiddleware().

    Now, our logger has been added as middleware, look at the work results of our logger within the console in a browser by deleting one of the articles.

    There are many other third-party middlewares available created by developers like redux-thunk, redux-saga, etc. We can combine multiple middlewares using the compose function. Let’s create a dummy middleware in store/index.js and see how we can use multiple middlewares:

    const dumbMiddleware = store => next => action =>
      next({ ...action, addition: "hello world" });
    
    const enhancer = compose(
      applyMiddleware(dumbMiddleware, logger),
      window.devToolsExtension ? window.devToolsExtension() : f => f
    );

    Remember, the order is important here. They are delivered by a chain from the first one to the last one, that’s why logger needs to be put at the very end, while technical middlewares – for example, random id generation – should be added at the beginning.

    You can visit the codesandbox below to see Lesson 11 part 1 and part 2 in action.


    Edit React_lesson_lesson11

    Home Task

    Create the function of adding a comment to an article: the place where comments are added below every article should also contain a form with a button that will enable the user to add a comment to the article. You do not need to store it anywhere else except for your store.

    Read More:  NextJS Tutorial: Getting Started with NextJS

    The lesson code is available in our repository.

    react react lesson
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Mohammad Shad Mirza
    • X (Twitter)
    • LinkedIn

    JavaScript lover working on React Native and committed to simplifying code for beginners. Apart from coding and blogging, I like spending my time sketching or writing with a cup of tea.

    Related Posts

    Streamlining Resource Allocation for Enhanced Project Success

    December 18, 2024

    Conducting Effective Post-Project Evaluations: A Guide

    December 16, 2024

    Strategies for Keeping Projects on Track and Meeting Deadlines

    December 10, 2024
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    Comics November 15, 2016

    True Freelancer’s story

    We are looking forward to meeting you on our website blog.soshace.com

    GraphQL

    October 5, 2017

    Building a Pokedex with Next.js

    January 12, 2021

    21. Уроки Node.js. Writable Поток Ответа res, Метод pipe. Pt.2

    October 26, 2016

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    Partnerships with Conferences: Announcement for 2019-2020

    Events September 19, 2019

    Strategies for Effectively Discussing Weaknesses in Interviews

    Interview November 24, 2024

    MSP Marketing Made Easy: 7 Proven Automation Tools

    MSP Lead Generation May 3, 2025

    8 Best Blogs for Entrepreneurs: Start Business, Grow Professionally, or Change Your Life

    Entrepreneurship April 18, 2019

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    Programming

    Python zip() Function Explained and Visualized

    Podcasts

    JavaScript and React Podcasts: The Ultimate Guide to Web Development Podcasts — Part 1

    JavaScript

    The Ultimate Introduction to Kafka with JavaScript

    Most Popular

    Структура организации

    Programming

    RxJS Methods. Part2

    Programming

    Node.js Lesson 9: Events, EventEmitter and Memory Leaks

    Node.js
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.