Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redux middleware -> intercept/block/transform?

we're running a SPA. dispatched actions (not components) are auth-based.

is it possible to intercept and transform dispatched actions?

i.e.

dispatch({
    type: 'FOO',
    payload: { some: value }
})

function magicMiddleware(action) => decide if authorized. 
if no...

dispatch({
    type: 'BAR',
    payload: { new: value }
})

else continue

note that 'FOO' should never hit a reducer

like image 645
Anthony Chung Avatar asked Mar 09 '23 14:03

Anthony Chung


1 Answers

Yes, that is absolutely one of the intended use cases for middleware. Any middleware can inspect and modify any action going through the pipeline before it reaches the reducers, and even prevent an action from continuing on.

You may want to read through the Middleware page in the Redux docs, as well as the articles in the Redux Middleware category in my React/Redux links list. Those should give you a better idea how middleware work, and how you can use them.

like image 147
markerikson Avatar answered Mar 18 '23 14:03

markerikson