0

Assume that an action creator (or a middleware) has a side-effect of starting a service: service.start().

The disable-action feature of redux-devtools reverts the state changes due to that specific action. How do we make sure that the side-effects are also reverted, which in the above example means calling service.stop()? I guess what is needed is an observer for the state, but I'm not sure if this is the flux/redux way of implementing it.

Panagiotis Panagi
  • 9,927
  • 7
  • 55
  • 103

1 Answers1

4

You can’t do this. There are no means of reverting side effects in Redux (e.g. how do you “undo” a GET request?).

Ultimately side effects shouldn’t affects the application unless they ultimately produce actions. (For example, a GET request ultimately produces an action with the response.) And when there are actions, they can be reverted.

So you can’t revert side effects, but this shouldn’t matter anyway. You can revert actions that are dispatched as the result, and nothing but actions can change application’s state.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • Indeed, if the application state is complete and only actions (not side effects) change state, there shouldn't be a need to revert the side effects. The only issue I have is that when working with the logger, the store state can go out of sync with the "actual" state. For example, an action logins a user and on success saves a cookie session. Disabling that action with the logger will correctly update the store state (user is logged out), but the cookie is still there. – Panagiotis Panagi Nov 19 '15 at 13:27
  • Normally you wouldn't use the same logic as in `react-devtools`, but instead call a logout (=== revertLogin) function. Looking at react-devtools code, perhaps it would be possible to write a middleware that responds to `{ TOGGLE_ACTION, ROLLBACK, ... }` actions and handles reverting side effects as needed. – Panagiotis Panagi Nov 19 '15 at 13:35
  • Yes (although that would be somewhat fragile because those are internal to DevTools and will break between versions). – Dan Abramov Nov 19 '15 at 23:52
  • Still this sounds like it could work. You can put this `applyMiddleware` call separately *after* DevTools enhancer in `compose` chain. – Dan Abramov Nov 19 '15 at 23:53