title: Redux middleware change state before next()
body: <p>I would like to modify the state before <code>next()</code> is called so every reducer applied after the middleware gets the new state. <strong>Is it possible? How?</strong></p>
  
  <p>The only idea that comes to my mind is very hacky and would be something like this:</p>
  
  <pre><code>export const myMiddleware = (store) =&gt; (next) =&gt; (action) =&gt; {
      const oldReducer = ????
      store.replaceReducer(myReducer);
      store.dispatch(action);
      const newState = store.getState();
      store.replaceReducer(oldReducer);
      return next(newState);
  }
  </code></pre>
  
  <p>As I haven't seen any method to get the current reducer, it should be given to the middleware in any manner:</p>
  
  <pre><code>export const myMiddleware = (oldReducer) =&gt; (store) =&gt; (next) =&gt; (action) =&gt; {
     ...
  }
  
  const store = createStore(originalReducer, applyMiddleware(myMiddleware(originalReducer)));
  </code></pre>
  
  <p>Which seems even more hacky!</p>
  
  <p>The main purpose is to build a package that maps an action object (<code>action.payload</code>) and a path (<code>action.meta</code>) in store <code>state</code>. In this scenario, the reducer is distributed in an npm package, so it should be "chained" somehow. So right now the reducer is detecting if there is a path and an object inside payload, and tries to reduce the new state from it.</p>
  
  <p>The worst solution is to instruct the user to call the reducer from their own reducer, just before any other action inside the reducer. This is not a solid pattern. So at first, I was thinking in an as much agnostic as possible middleware that automatically does the work. And that's why I'm trying to modify state from middleware if possible.</p>