Home > OS >  Thunk Middleware: how/why does action creator have access to dispatch when it's not passed in?
Thunk Middleware: how/why does action creator have access to dispatch when it's not passed in?

Time:01-16

For reference, this is the origin of the below screenshots: enter image description here

enter image description here

CodePudding user response:

@JLRishe answered this correctly in a comment.

fetchUsers is a function that returns an anonymous function. The anonymous function takes the dispatch function as an argument. When you call store.dispatch(fetchUsers()), note that you're passing the return value of fetchUsers to store.dispatch. Redux then calls this anonymous function, passing it the dispatch method as an argument. The anonymous function is then invoked and can use the passed in dispatch function to dispatch additional actions.

  • Related