From the course: Building Modern Projects with React
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Creating a thunk
- [Narrator] Now it's time to create our first thunk. Inside our to-do folder, let's create a new file called thunks.js and this is where we'll put the thunks for our project. So what does a thunk actually look like? Well, in Redux, a thunk is simply a function that returns another function, which contains the actual logic that we want to perform when it's triggered. So for our first thunk, let's do something fairly simple. We're going to define a thunk called display alert. So we'll say export const displayAlert, and what it's going to do is simply return another function that displays an alert on the user's screen. So we'll say alert, hello. So this is about as simple as thunks can get. We'll see later on how we can add a lot more functionality to our thunks, but first let's see how to dispatch a thunk. You might be happy to find out that the way we dispatch a thunk is nearly identical to the way that we dispatch…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Why do you need Redux Thunk?1m 57s
-
How does Redux Thunk work?2m 51s
-
Adding Redux Thunk to React2m 30s
-
Creating a thunk3m 30s
-
The Todos API2m 18s
-
Async thunks5m 56s
-
Adding another reducer7m 19s
-
Refactoring the todos reducer2m 50s
-
Using thunks to create server resources5m 58s
-
Using thunks to delete server resources4m 46s
-
Challenge: Using thunks to update server resources1m 14s
-
Solution: Using thunks to update server resources3m 34s
-
-
-
-
-