课程: React: Creating and Hosting a Full-Stack Site (2022)
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Adding data loading to page components
课程: React: Creating and Hosting a Full-Stack Site (2022)
Adding data loading to page components
- [Instructor] All right. So now that we're sure that our use effect hook logic is getting called at the right time, all we really have to do is, instead of setting our article info state to fake random data as we've been doing, we just need to load this data from the server and call set article info with the response that we get from the server instead. So the first thing that we're going to want to do here, of course, is import the axios package. And to do that, we just need to say import axios from axios and then down here, inside use effect, what we're going to do is use the axios.get function to make a get request to the server endpoint that will give us back all of the info about the article. So what that's going to look like we're going to say const response equals await axios.get and we're going to put in the URL of our server, which is going to be https://localhost:8000/api/articles, and then we're going to add the…