1

I wrote a middleware, like as blow :

const testMiddleware = ({ dispatch, getState }) => next => action => {
    console.log('test middleware')
};

export default testMiddleware;

and added it to my store: applyMiddleware(testMiddleware) in ever action, i get test middleware in my console.

and i wrote a simple action, like this:

export const sayHi = () => {
    return dispatch => {
        console.log('hi');
    }
}

How can i dispatch sayHi action in my middleware?

Masoud92m
  • 602
  • 1
  • 8
  • 24

1 Answers1

3

Middleware gets the store’s getState() and dispatch() functions as first argument, so you can do the following (after importing required actions):

const middleware = ({dispatch, getState }) =>{
    return next => action => {
        dispatch(someAction);
        // return data;
        return next(action);
  }
}

So, the middleware signature is ({ getState, dispatch }) => next => action.

https://redux.js.org/api/applymiddleware

IfSoGirl
  • 161
  • 1
  • 7
  • what is `data` in `return data` ? i getting error `InternalError: too much recursion` – Masoud92m Mar 20 '19 at 19:27
  • Of course you get an error cause data is undefined variable, that was just an example) you should return action as mentioned in the link – IfSoGirl Mar 21 '19 at 03:45
  • thank you, i wrote like your new answer, but i get error `InternalError: too much recursion` please check your chat. – Masoud92m Mar 21 '19 at 08:10