So, first of all, we're going to be talking about this, the basic requests. Then we're going to talk about the state and about the advanced features like paging and other things that I talked about earlier.
So, when we're fetching data, it doesn't matter, it can be React, it can be Angular, anything you want, we typically have three options how we can get data from a remote endpoint. First, the old one is XMLHttpRequest. Nobody uses that these days, I think, in its raw form because we have Axios and we have Fetch API.
Now, Axios and Fetch API are slightly different, but they all do the same thing, they just want to make the data fetching easier and better for developers. Now, Axios... when we're talking about the raw requests and responses, we have two options, pretty much the Axios and Fetch API. The difference is that Fetch API, you see, it's really easy to use. This is the only thing that you need to do. The biggest disadvantage, or you could call it the disadvantage, is that it's not available in all browsers, but otherwise it's a native API, so you don't have to install any package, you don't have to do anything. It's all... it's already available in all the browsers.
So Fetch API is something that you should be using by default, and even though it's not available in all browsers, and by all browsers, I mean Internet Explorer version 11. If you need to, you know, work on projects that have to support this browser, I'm sorry for you, but there is a polyfill that you can use to actually bring the Fetch functionality into those browsers as well, yeah? So this is also a solution if you want to use Fetch. Now, this is a screenshot from Can I use? You see that it's pretty much all green, only the Internet Explorer here, but otherwise, all the current browsers and mobile browsers, and also Node.js, they support Fetch API by default, and you don't have to install anything. In Node.js, the support came early last year. So you have to use Node.js at least 17.5, but that should be always the case these days. And as I mentioned, there is no need for extra plugins. Now, of course, you're typically able to handle 85-90 percent of all the cases with just Fetch API. There might be some cases where we're going to need a bit more than that. But that brings me to a first task that I have for you. And that is to actually start working on the application and see it up and running. So the first task is to add the following Fetch call to the component slash Fetch TSX. I'm going to show you how to do that. So when we open the Visual Studio, there is a component under source and components called Fetch TSX. And you see that it's showing some errors because there is a Fetch call missing. So we're just going to use that. We're going to add that here. Make sure to keep the S responses, blah blah blah there because it's still using the typed data from the CMS. And we're going to do here await Fetch. And here you see that we only need to provide the endpoint URL, yeah? In this case, if you're using the remote endpoint, it's going to be the URL on Netlify. And of course we're going to have to resolve the JSON here. So, something like that should work. So first of all, we're awaiting fetch, just the fetch. It doesn't need any import, anything. It's just the fetch. We're fetching the URL. And then we're doing another await for JSON. So we need to convert the string into an object. So that's why we're doing JSON here. Of course, we could do with, we don't have to use async await. We could do with promises to do a dot then and so on. I just prefer this notation as it seems a bit more clear. But that will be the first task for you to do this. And when it's actually saved, you can go back into the apt-tsx. And as I mentioned before, just before the closing tag of table, put the component there. And of course, there is no import, so it's going to want that import from us. Yeah, add import from components and fetch. And save that. Now, I was always using the defy def, but I think that NPM run-def should work too. Or not. Okay, maybe NPM run start. Let me check. Okay, let me check. Yeah, NPM run start should work. Okay, and the first, the first problems, yeah, there are components that it doesn't like because it cannot compile them, so we might need to comment them. XCLS, XCLS, React query. React query. So, if you're using this, if you're using NPM run start, we're probably not gonna have to change that. If you have netlify-cli installed, if you run netlify-def, I think it works, even though there are problems, but let me check. Otherwise, we're just gonna have to rename the files so it doesn't pick them up as TypeScript files. Yeah, he doesn't like them too. Okay, sorry for that. We're, that's one more step that we have to do. So, the files that are not able to be compiled, we're just gonna have to rename them. NPM run start. Are these up? Oh, this will tell me which they are. Oh, okay, now it works.
Comments