Skip to content
This repository was archived by the owner on Dec 15, 2018. It is now read-only.
This repository was archived by the owner on Dec 15, 2018. It is now read-only.

Dispatch in middleware does not use store-enhancer dispatch #36

@dpwrussell

Description

@dpwrussell

Hi,

I am experimenting with this:

Your custom middleware can intercept this action to dispatch new actions in response to URL changes.

I have written a barebones middleware for routes with auth in their result. Like so:

export const auth = store => next => action => {

  if (
    action.type === LOCATION_CHANGED &&
    action.payload &&
    action.payload.result &&
    action.payload.result.auth
  ) {

    const loggedIn = selectors.isLoggedIn(store.getState());

    if (!loggedIn) {
      setTimeout(() => {
        store.dispatch({
          type: PUSH,
          payload: '/login'
        });
        // Other dispatch which works perfectly
        // store.dispatch(fetchVocabsIfNeeded());
      }, 3000);
    }
  }
  return next(action);
};

The setTimeout is just to simulate an async fetch that I would usually dispatch to the server to see if I had an active session. If not, then I want to go to the login page.

This seems to generate a ROUTER_PUSH action with a payload of '/login'. The route does not change and a fragment for '/login' is not displayed. However, if I dispatch that same action from an onClick on my App component, the result is a ROUTER_LOCATION_CHANGE with the payload of pathname, key, route, params, etc, and it works fine.

I am not experienced with redux middleware and suspect that this has something to do with the middleware chain and where redux-little-router sits in that chain, but I'm unsure how to address this. Should this be possible?

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions