Redux provides us with thunk-middleware which allows us to dispatch thunk functions. Thunk functions always take dispatch and getState as arguments (additional args can also be configured).
So there are two cases here:
Case I: dispatching action objects
const actionObject = dispatch(actionObject)
By default, store.dispatch(action) returns the actual action object.
here
Case II: dispatching thunk functions
First thing you need to know is the difference between thunk functions and thunk action creators. Which is very well defined here, writing-thunks and a little snippet is:
// fetchTodoById is the "thunk action creator"
export function fetchTodoById(todoId) {
// fetchTodoByIdThunk is the "thunk function"
return async function fetchTodoByIdThunk(dispatch, getState) {
const response = await client.get(`/fakeApi/todo/${todoId}`)
dispatch(todosLoaded(response.todos))
}
}
Now you have two choices, either dispatch the fetchTodoByIdThunk
(but you can't in this case as it uses todoId
but if it was defined statically with value in place of todoId
, then you can dispatch it) using dispatch(fetchTodoByIdThunk)
or dispatch fetchTodoById
using dispatch(fetchTodoById())
, it's the same but the latter can take arguments for further refining. The thunk middleware will take care of the intercepting this function passed to dispatch and will call this function with dispatch and getState as arguments.
What I found is that they use the word thunk
for thunk action creators. You can see it just after the above snippet on writing thunks (see the title of next snippet). From all this info, the only thing I came up with is datsApi.endpoints.getData.initiate
returns a thunk action creator, which when called return the thunk function, which is then dispatched.