Here's the transcript transformed into Markdown format:
Little Lemon Restaurant Competition
The Little Lemon Restaurant wants to run a competition for its customers where a lucky customer will receive a free meal at the restaurant. All customers that are signed up to the Little Lemon app will be entered into the competition, and a random customer will be selected as the winner. In this video, I will show you how to get some random user data from a website. I will be using the random.me website to access random user data for this demonstration.
Code Demonstration
I have prepared some code for this app to fetch the user data from the website. If I execute this code, it will
initially output the "data pending" text in the H1 heading. At the same time, in the background, it will be carrying
out the fetchData function to retrieve the user information from the random user website. Notice that I have the dev
tools open and the Network tab active. I will click on the North router link dropdown to artificially slow down my
connection. In the dropdown, I will choose the slow 3G preset. This is so that I can observe the "data pending" text in
the heading before the data gets fetched from the web. Once the data has been successfully fetched, it updates the view
with the data returned in the H1 heading and the user information that has been retrieved. In this example, the data
that was requested was the first name and the last name of this random user.
Code Breakdown
Let's step through this code in more detail:
const App = () => {
const [userData, setUserData] = useState([]);
const fetchData = async () => {
const response = await fetch('https://randomuser.name/api');
const data = await response.json();
setUserData(data);
};
useEffect(() => {
fetchData();
}, []);
return userData.length > 0 ? (
<div>
<h1>
{userData[0].name.first} {userData[0].name.last}
</h1>
<h2>{userData[0].email}</h2>
<h2>{userData[0].phone}</h2>
</div>
) : (
<h1>Data Pending</h1>
);
};- useState Hook: Inside the
Appfunction, I'm invoking theuseStatehook at the top level of the component. The initial value of the state variable is an empty array. - fetchData Function: I have defined the
fetchDatafunction, which is fetching data from therandomuser.nameAPI. It retrieves the response from the API in JSON format and then updates the state variable with this JSON data. I'm not using a hook inside thefetchDatafunction because that's against the rules of hooks. - useEffect Hook: I'm calling the
useEffecthook and from inside theuseEffecthook, I'm calling thefetchDatafunction which I've defined previously. - Conditional Logic: Finally, I'm using conditional logic to decide what to return. First, I'm using the
Object.keyscode snippet to put all the keys of the user object into an array. SinceObject.keysreturns an array, I can access the length property on this array and check if the length of this array is greater than zero. If it is, it means that the contents of the state array has changed because as you may remember, the state variables array was empty. So if the array is no longer empty, then the div section will be returned with theH1tag and a couple ofH2tags. Otherwise, theH1tag below is returned that reads "Data Pending". Sometimes it can take a little bit of time for thefetchDatafunction to retrieve the data requested. Therefore, the "Data Pending" message will be displayed initially after the code is executed. Once the data has arrived from thefetchDatacall, this change in state causes a re-render of my component. So the return statement's ternary operator is re-evaluated and that returns all the data from my call to the fetch API.
Conclusion
This is essentially how you get data from the web using React. So the Little Lemon Restaurant would be able to apply the
same logic to their customer list API to select a random winner for their competition. In this video, you have learned
how to fetch data using the useState and useEffect hooks.