tshaddix / webext-redux

A set of utilities for building Redux applications in Web Extensions.
MIT License
1.23k stars 180 forks source link

Proxy store getState not properly bound to store in applyMiddleware #157

Closed marcokam closed 6 years ago

marcokam commented 6 years ago

When using middleware that exposes getState, an error is occurring when the method is called:

Uncaught (in promise) TypeError: Cannot read property 'state' of undefined

Here is some code that reproduces the issue:

const middleware = [thunkMiddleware];
const store = applyMiddleware(new Store({ portName: "test" }), ...middleware);
store.dispatch((dispatch, getState) => {
    getState();
});

The applyMiddleware function in this project is very similar to the one in https://github.com/reduxjs/redux, but the difference is that redux doesn't use classes or rely on this in their store to access the current state, so in that project the method does not need to be properly bound to the store.

A fix can be made by changing this line to:

getState: store.getState.bind(store)

I'm planning to work on a PR this weekend to fix this, and also perhaps to tackle #139 as well.

tshaddix commented 6 years ago

@marcokam Awesome - thanks for pushing for a fix on this.