Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making dispatch + store available to middleware arguments. #74

Closed
andrewmclagan opened this issue May 25, 2016 · 2 comments
Closed

Making dispatch + store available to middleware arguments. #74

andrewmclagan opened this issue May 25, 2016 · 2 comments

Comments

@andrewmclagan
Copy link

andrewmclagan commented May 25, 2016

When adding an extra argument, such as an API helper. Is it possible to make the store and dispatch available to the argument.

e.g. Adding a token from the store to all API requests.

const reduxThunkMiddleware = thunk.withExtraArgument(api);

and in the api argument:

// ...
function api(dispatch, getState) {
    return fetch('/authenticated/route', { token: getState().token });
}
// ...
@andrewmclagan andrewmclagan changed the title Making the store available to middleware arguments. Making dispatch + store available to middleware arguments. May 30, 2016
@amirvt
Copy link

amirvt commented Jul 5, 2016

This is what i used:

const api = new Api();
const store = createStore(
  reducer,
  applyMiddleware(thunk.withExtraArgument(api))
)
api.setDispatch(store.dispatch);

and then in Api:

someApiMethod(){
  this._dispatch(someAction);
}

@timdorr
Copy link
Member

timdorr commented May 25, 2018

You can do this by passing them into your extra arg:

const api = (dispatch, getState) => fetch('/api', { token: getState().token }))

const store = createStore(
  reducer,
  applyMiddleware(thunk.withExtraArgument(api))
)

// later
function fetchUser(id) {
  return (dispatch, getState, api) => {
    api(dispatch, getState)
  }
}

@timdorr timdorr closed this as completed May 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants