Skip to content
This repository has been archived by the owner on Oct 26, 2018. It is now read-only.

dispatch when handling LOCATION_CHANGE action in custom reducer causes triple push to history #577

Closed
ptacekj opened this issue May 17, 2017 · 2 comments

Comments

@ptacekj
Copy link

ptacekj commented May 17, 2017

Hi,

I found a weird behavior when I need to dispatch another action while handling LOCATION_CHANGE

var changeMiddleware = (store) => {
  return (next) => {
    return (action) => {
    	/** there can be multiple other action types detected who will dispatch TRACK_IT action */
      if (action.type === LOCATION_CHANGE) {
        store.dispatch({
          type: 'TRACK_IT'
        });
      }
      return next(action);
    };
  };
};

The reason for this is to be able to aggregate TRACK_IT actions with rxjs and handle them in bulks.

When I go from page A to page B it will result in following navigation changes:
A -> B
B -> A
A -> B

This only happens with adjustUrlOnReplay: true and only if there is another action dispatched before return next(action)

I have no idea if I have it setup incorrectly or if I'm just doing something wrong.

Fiddle with demonstration is https://jsfiddle.net/akhoegsk/ just click on the 3 links one after one a then click back a few times. It should behave like browser back button but instead it just cycles between the last 2 There are listenBefore and listen handlers setup.

Thank you for any information
Jan

@behnaam
Copy link

behnaam commented Sep 8, 2017

@ptacekj Seems to be due to the fact that the inner store.dispatch is using a non-enhanced dispatch. See this open bug in redux bug tracker.

@timdorr
Copy link
Member

timdorr commented Sep 8, 2017

This is correct. I would setTimeout there to cause the dispatch to occur after the running dispatch has resolved.

@timdorr timdorr closed this as completed Sep 8, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants