From the course: Building Modern Projects with React

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Async thunks

Async thunks

From the course: Building Modern Projects with React

Start my 1-month free trial

Async thunks

- [Instructor] Now that we've seen all the endpoints of our Todos API, let's hook up our React application to this server by using thunks. The first thing we'll do is open up our thunks.js file. And we're going to create a new thunk. We'll say export const and we'll call this one loadTodos and it won't take any arguments and it'll return an async function which you're allowed to do here. And we're going to put our loading logic into this thunk. So before we implement the body of this async function that we're returning, there's something you should know about thunks. The function we return here gets passed two arguments when the thunk is triggered. And those two arguments are dispatch, which we can use to dispatch other redux actions from inside our thunk, and getState, which is a function that we can use to get access to the current state of the redux store. These two arguments are quite useful. In this particular thunk…

Contents