grochadc / use-reducer-middleware

An extender useReducer react hook that accepts middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

useReducerMiddleware

This is a custom hook that extends native react useReducer to take middleware as its third argument.

Usage

const middleware = [
  (store) => (next) => (action) => {
    //store is the tuple returned by useReducer
    const [state, dispatch] = store;

    //Do something when an action is dispatched
    console.log("Action dispatched ", action);

    //pass the action to the next middleware (action can be modified before passing it)
    return next(action);
  },
];
const { state, dispatch } = useReducerMiddleware(
  reducer,
  initialValues,
  middleware
);

The way middleware should be written in this package is similar to redux's own middleware implementation.

About

An extender useReducer react hook that accepts middleware


Languages

Language:TypeScript 95.6%Language:JavaScript 4.4%