0

I think Redux is pretty useful for debugging because it tracking what actions were dispatched by your app in its dev tools. Is it bad practice to dispatch an action with no intention of handling it with a reducer?

For example, I want to make an AJAX call and update local react component state based on the response, but still want a record of this call in the dev tools for debugging purposes. I would like to create an action creator, connect it to my component with the react-redux mapDispatchToProps property in react-redux's connect wrapper, but just never have a case in my reducer function that handles this action.

alanrzhang
  • 199
  • 1
  • 1
  • 7
  • I'll say I'm doing that also but I don't see any problem yet, as long as you do not want to watch whether the action is success or not you don't need the reducer. It's just like running a function without a return value. – Andus Feb 25 '20 at 02:05

1 Answers1

2

Not necessarily. A common example is firing an action which a middleware intercepts, interprets, and based on the contents of the action may or may not fire one or more subsequent actions, and may even swallow the original action you fired (i.e. not forward it further in the chain, so it in fact never reaches any reducers).

Whether it swallows the action or not, there will be plenty of cases where you're only interested in the subsequent actions fired by the middleware, so may not handle that original action in any reducer anyway.

This pattern can be seen for example in redux-promise-middleware

The usecase you're describing here, though, using an action purely for logging purposes, I would advise against. It's not the worst thing in the world, but really a Redux action should do something - using it just to log that something happened like this is probably a misuse, and overly confusing (imagine someone maintaining this code, spending a while trying to figure out if this action does anything, then if the fact it doesn't do anything is a bug or deliberate, etc). Just using console.log is much simpler and clearer.

davnicwil
  • 28,487
  • 16
  • 107
  • 123