Git Product home page Git Product logo

async-redux's Introduction

Redux Middleware

Agenda

  • Why might we need middleware?
  • What is redux middleware?
  • Compose

Resources

Why?

Redux middleware are functions that sit between a dispatched action and the reducers. They allow us to customize behavior based on the action. They also allow us to customize behavior after the reducers finish.

For example:

  • we can log every action that redux handles
  • we can wait for asynchronous actions
  • we can store state in local storage

What?

Redux middleware takes the form

const myMiddleware = store => next => action => {
  // ...do stuff here
}
parameter description
store the redux store with all of its methods
next a function to move to the next middleware (returns new state)
action the incoming (dispatched) action

Middleware can be applied when you create your store:

import {
  createStore,
  applyMiddleware
} from 'redux';
import reducer from './reducers';
import { myMiddleware } from './middleware/myMiddleware.js';

export default createStore(
  reducer,
  applyMiddleware(
    myMiddleware
  )
);

Compose

When we applied middleware before we applied a store enhancer. Middleware isn't a fundamental part of redux, but is added as an enhancement. We've used another redux enhancement when we used redux devtools.

To add multiple enhancements we need to compose the enhancements together.

import {
  createStore,
  applyMiddleware,
  compose
} from 'redux';
import reducer from './reducers';
import { myMiddleware } from './middleware/myMiddleware.js';

export default createStore(
  reducer,
  compose(
    applyMiddleware(
      myMiddleware
    ),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  )
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.