-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add withExtraArgument() #70
Conversation
Semi-stupid question: if you're going to add the ability to augment with one extra argument, why not make it multiple extra arguments? |
I don’t see much of a point to that. You can always destructure if you need them: const store = createStore(
reducer,
applyMiddleware(thunk.withExtraArgument({ api, whatever }))
)
// later
function fetchUser(id) {
return (dispatch, getState, { api, whatever }) => {
// you can use api here
}
} Just one argument seems conceptually simpler than having to explain that the first two arguments are from Redux, and any number of next arguments are your custom ones. |
Okay, yeah, that seems pretty viable. Nice. |
Could someone open up why this was implemented? I can't come up with any valid reason/use case why to use this over es6 modules. |
@villesau Useful for mocking an API client in tests, for example. |
Testing and being able to provide a separate pre-initialized API client instance for every request in server rendering. |
People often request the ability to inject a custom argument into thunks. While we don’t plan to do this by default so that we don’t have to change the signature of
redux-thunk
, we are addingthunk.withExtraArgument(arg)
as a way to inject an arbitrary argument into every thunk action creator. This argument will be available as the third argument in the action creators, right aftergetState()
.Example:
I chose
withExtraArgument
over something more compact likeinject
to make it clear we’re not mutatingredux-thunk
itself, and that this argument will be passed as an extra argument (rather than, for example, instead of, the two usual arguments).