JavaScript Redux: turn middleware on and off

I am looking for a way to turn a middleware on and off. I introduced a tutorial functionality - I listen to what the user is doing with the UI by checking each action with a "guidance" middleware. if the user clicks on the right place he moves to the next step in the tutorial. However this behaviour is only needed when the tutorial mode is on. Any ideas?

const store = createStore(holoApp, compose(applyMiddleware(timestamp, ReduxThunk, autosave, guidance),
window.devToolsExtension ? window.devToolsExtension() : f => f)); 

for now my solution was to keep the "on" switch in a guidanceState reducer and dirty check it in the middleware:

const guidance = store => next => action => {

    let result = next(action)

    const state = store.getState();
    const { guidanceState } = state;
    const { on } = guidanceState;

    if (on) {

 ....

However, ~95% of the time the tutorial mode would be off so dirty checking every action all the time feels a bit, well, dirty... ;) Any other ways?

Answer:1

I don't know of any way to replace middlewares on the fly via redux's API.

Instead, you could create a completely new store with the old store's state as initial state and the new set of middlewares. This may work seamlessly with your application.

Answer:2

I have a bunch of polygons which are stored in a database. I would like to add them to the map in such a way that they can be edited using the leaflet-draw toolbar. Although, now the polygons get ...

I have a bunch of polygons which are stored in a database. I would like to add them to the map in such a way that they can be edited using the leaflet-draw toolbar. Although, now the polygons get ...

Can the localStorage be cleared for a specific application? The usecase is the following: - we have a self built application called "Agent Cockpit" which is sharing the same domain name like ...

Can the localStorage be cleared for a specific application? The usecase is the following: - we have a self built application called "Agent Cockpit" which is sharing the same domain name like ...

  1. localstorage clear specific key
  2. localstorage clear specific item
  3. localstorage clear specific

I have a page where an anchor tag has id. When the anchor tag is clicked I am fetching the value to input type hidden and after again clicking on the same div it should be removed. The selection will ...

I have a page where an anchor tag has id. When the anchor tag is clicked I am fetching the value to input type hidden and after again clicking on the same div it should be removed. The selection will ...

  1. value onclick javascript
  2. value onclick
  3. value onclick submit

Using redux and react router, I would like to access a route parameter on a component other than the one specified by the route. I've got react-router, redux, and react-router-redux set up as ...

Using redux and react router, I would like to access a route parameter on a component other than the one specified by the route. I've got react-router, redux, and react-router-redux set up as ...

  1. react router route params
  2. react router route props
  3. react router route component
  4. react router route render
  5. react router route path
  6. react router route exact
  7. react router route config
  8. react router route key
  9. react router route change event
  10. react router route programmatically
  11. react router route matching
  12. react router route query params
  13. react router route vs link
  14. react router route on click
  15. react router route guard
  16. react router route not found
  17. react router route name
  18. react router route api
  19. react router route change
  20. react router route onenter