Loading
Lesson 12
Courses / Introduction to Redux Saga
Handling the Loading State with a Flag - Learn Redux Saga Tutorial

Video Transcript

Welcome back. So we got the fetch fruits okay for the succeeded case. We should do a little bit more about the loading state and then error handling. Let's start with the loading state. Right now currently it relies on the fact that fruits is initially no in order to show the loading message. That might not always be the case. So what we have to do is when we make, when we dispatch fetch fruits requested that means that the request is pending. So that means we have to tell, we have to have an indicator, a flag saying that the request is currently pending and we can then also use that to display the loading message. In case that fruits is already defined but somehow we made a request to load it again or fetch a new list of fruits. So we need only add a property with the pending or loading flag to the reducer here. If you go to the reducer then in the initial state you add a property called say I don't know, fruits, pending or loading whatever name you want to call it. Let's say fruits loading. Let's say it's initially false because we don't have any request to start with. But the moment we make the request that is when we have the fetch fruits requested action time that's when we set fruits loading to true. Then if the request succeeds or it fails we set it back to false. So here it's going to be pretty simple in the reducer. You're going to handle another case. If you get fetch fruits requested all you have to do is return the copy of the state. Let's do object assign, object assign, create an empty object, copy from the state. Then fruits loading you set it to true. Okay, notice this is in the reducer. It's not in the saga, right? So the moment we dispatch this action, first it goes through the saga because it's a middleware and does the HTTP call to find all. Then it goes here and then it's going to set fruits loading to true. Then when we succeed then you set it back to false. Fault loading is false here. We put it in a new line. You can see better. Okay. Then we can use this property in the app.js if you go back there. Notice we have this check if fruits is null. It's not defined. We can also add or right here this.probs.fruits.still loading. We keep displaying the loading message. In order to be able to access fruits loading from the application state you have to go down there to map state to props and add the fruits loading property which comes from where? From state.fruits.loading. Okay. That's it. We have access to fruits loading. To illustrate this working we can, to illustrate that this is actually working, we can set the fruits list in the reducer initial state. Instead of saying no right here for the fruits you can set it to an empty list. So that will be defined. Then the app is going to check for the second condition because the first one is going to be what? This is going to be empty array which is defined. Then it becomes a false value and then it's going to check the second one fruits loading. So let's see how it goes. You can see it loading fruits and it did it. Okay. And you can see here in the console log for the this.props you can trace it back and you can see that the first one here this props console log from the render function of the app component gives fruits loading files initially. Then it makes a request for the fruits. Then it changes fruits loading from false right to true. Here there's the props and then at the end when we get the succeeded action it changes you can see there's the props fruits loading back to false. So we don't have that loading state anymore. Okay. And that happens here after succeeded. So let's go back here. That's what we did. Now the second thing we have to do is to handle the error. For example if you go back to your API.js we're always resolving the promise here for find all with a value but that might not always be the case right because requests can fail. So let's simulate an error instead of resolving I'm going to comment this out I'm going to do reject. And I'm going to create a new error. I'm going to give this message to this error saying that failed it failed to fetch fruit data could be any message you want. Okay. So this is going to simulate a error from the find all. So it's like we failed the request after two seconds. Now let's see what happens in that case. So we get a loading state that never it gets stuck right never gets out of the loading state. Then we get this uncought at root saga root saga at the method fetch fruits. So there is a narrow that was not caught in our saga function generator function. And that's what we're going to learn in the next lesson. We're going to learn how to implement error handling. See you then.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: