router5/redux-router5

Update url parameters from middleware

Closed this issue · 1 comments

User open url with default parameters. In store I have data which must be in url too. I tried to use middleware for this issue in the next way:

const induceMiddleware = store => next => action => {
  if (action.type === actionTypes.TRANSITION_SUCCESS) {
    if (isUrlHaveEnoughParams(action.payload.route.params)) {
      const nextResult = next(action);
      // do some stuff
      return nextResult;
    }

    const nextResult = next(action);
    setTimeout(() => {
      store.dispatch(actions.navigateTo(
        'group.product',
        fitRouteParams(store.getState(), action.payload.route.params),
        { replace: true })
      );
    });
    return nextResult;
  }
  return next(action);
};

It's works only when I add timeout. Without timeout navigateTo runs two times with old and new parameters.

Can you help me to find a correct way to solve this problem?

troch commented

If I understand well, you want default parameters for your routes, If so, I wouldn't deal with it with redux middlewares. I would leave it to the router, using a plugin. I encourage you to look at: https://github.com/router5/router5-persistent-params. Basically, what you need to do is to decorate your router instance buildPath and buildState methods.