Skip to content

Commit f4d80f1

Browse files
committed
Keep dispatch available while middleware is initializing (#1647)
* Revert "remove initial assignment of `dispatch` in applyMiddleware to make it more expressive" This reverts commit 9496fd7. * Add test verifying existing behavior
1 parent 655fe6a commit f4d80f1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/applyMiddleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import compose from './compose'
1919
export default function applyMiddleware(...middlewares) {
2020
return (createStore) => (reducer, initialState, enhancer) => {
2121
var store = createStore(reducer, initialState, enhancer)
22-
var dispatch
22+
var dispatch = store.dispatch
2323
var chain = []
2424

2525
var middlewareAPI = {

test/applyMiddleware.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,22 @@ describe('applyMiddleware', () => {
9494
done()
9595
})
9696
})
97+
98+
it('keeps unwrapped dispatch available while middleware is initializing', () => {
99+
// This is documenting the existing behavior in Redux 3.x.
100+
// We plan to forbid this in Redux 4.x.
101+
102+
function earlyDispatch({ dispatch }) {
103+
dispatch(addTodo('Hello'))
104+
return () => action => action
105+
}
106+
107+
const store = createStore(reducers.todos, applyMiddleware(earlyDispatch))
108+
expect(store.getState()).toEqual([
109+
{
110+
id: 1,
111+
text: 'Hello'
112+
}
113+
])
114+
})
97115
})

0 commit comments

Comments
 (0)