In this task, you will implement the Covid dashboard. The dashboard will display the total number of cases, active cases, and recoveries for a selected country. To achieve this, we will incrementally build the dashboard by adding state to the SelectCountry and DisplayStatistics components. We will also fetch the country data and Covid data from the APIs.
Step 1: Add state to the SelectCountry component
In this step, we will add state to the SelectCountry component to store the selected country. Open the src/components/select-country.tsx file and add the following code:
Notice that we have added state to store the selected country and the list of countries. We have also added a handleOnCountryChange function to update the selected country when the user selects a country from the dropdown.
The key attribute in the SelectItem component
In the SelectItem component, we have added a key attribute with the value of country.code. This is important when rendering a list of items in React. The key attribute helps React identify which items have changed, are added, or are removed. It should be a unique value for each item in the list.
If you don’t provide a unique key attribute, React will throw a warning in the browser console. The key attribute should be a string or a number and should be unique among siblings.
Step 2: Add state to the DisplayStatistics component
In this step, we will add state to the DisplayStatistics component to store the Covid data for the selected country. Open the src/components/display-statistics.tsx file and add the following code:
In this code snippet, we have added state to store the Covid data for the selected country. We have also added a STATISTICS array to define the statistics we want to display. We then map over this array to display the statistics in separate cards.
Step 3: Fetch country data in the SelectCountry component
Now that we have added state to the SelectCountry component, we can fetch the list of countries from the API. Open the src/components/select-country.tsx file and add the following code:
In this code snippet, we have added an useEffect hook to fetch the list of countries when the component mounts. The fetchCountries function is an asynchronous function that fetches the list of countries from the API. We then update the state with the fetched data. This API related function is imported from the api file in the data folder.
The useEffect hook
The useEffect hook is used to perform side effects in function components. It runs after the component has rendered and can be used to fetch data, subscribe to events, or perform cleanup. The useEffect hook takes two arguments: a function and an array of dependencies. The function is the side effect you want to perform, and the array of dependencies is used to control when the side effect runs.
In this case, we want to fetch the list of countries when the component mounts, so we pass an empty array as the second argument to the useEffect hook. This tells React to run the effect only once when the component mounts.
It is a common pattern to use the useEffect hook to fetch data from an API when the component mounts. This ensures that the data is fetched only once when the component is rendered for the first time.
Step 4: Fetch Covid data in the DisplayStatistics component
Now that we have added state to the DisplayStatistics component, we can fetch the Covid data for the selected country. Open the src/components/display-statistics.tsx file and add the following code:
In this code snippet, we have added an useEffect hook to fetch the Covid data for the selected country when the component mounts. We are fetching the Covid data for the United States (US) as a placeholder. We will update this to fetch the Covid data for the selected country in the next step.
Step 5: Pass the selected country to the DisplayStatistics component
In this step, we will pass the selected country from the SelectCountry component to the DisplayStatistics component. Open the src/App.tsx file and update the code as follows:
In this code snippet, we have passed the countryCodeprop to the DisplayStatistics component with the value "US". This is a placeholder value that we will update in the next step.
We should now update the DisplayStatistics component to accept the countryCode prop and fetch the Covid data for the selected country. Open the src/components/display-statistics.tsx file and update the code as follows:
In this code snippet, we have added a countryCodeprop to the DisplayStatistics component. We have also updated the useEffect hook to fetch the Covid data for the selected country when the countryCode prop changes. We have also added a conditional check to return null if the countryCode is null.
Notice the type definition for the DisplayStatisticsProps interface. This is a common pattern in React to define the props that a component accepts. It helps with type checking and provides better documentation for the component.
Component Props
In React, components can accept props (short for properties) that are used to pass data from a parent component to a child component. Props are read-only and should not be modified by the child component. They are used to configure the child component and can be used to pass data, functions, or other values.
When you pass props to a component, you can access them inside the component using the props object. For example, if you pass a prop named name to a component, you can access it inside the component as props.name. In the case of functional components, you can destructure the props object to access individual props directly. For example, if you have a prop named name, you can destructure it as { name } in the function arguments. This is a common pattern in React to make the code more readable and concise.
You pass a prop to a component by adding an attribute to the component tag. For example, <DisplayStatistics countryCode={"US"} /> passes the countryCode prop with the value "US" to the DisplayStatistics component. You can pass any value as a prop, including strings, numbers, objects, functions, or even other components.
Step 6: Set the country code in the SelectCountry component
In this step, we modify the code so that when the user selects a country from the dropdown, the country code is set in the App component. Open the src/App.tsx file and update the code as follows:
Notice how we pass a function setCountryCode to the SelectCountry component as a prop. This function is used to update the countryCode state in the App component when the user selects a country from the dropdown.
Next, update src/components/select-country.tsx to call the setCountryCode function when the user selects a country:
In this code snippet, we have added a setCountryCodeprop to the SelectCountry component. We call this function with the selected country code when the user selects a country from the dropdown. This function is passed from the App component and is used to update the countryCode state in the App component.
Lifting State Up
In React, when multiple components need to share the same state, you can lift the state up to a common ancestor component. This is known as lifting state up and is a common pattern in React to manage shared state between components. By lifting the state up to a common ancestor component, you can ensure that the state is consistent across all components that need to access or update it.
In our case, we are lifting the countryCode state up to the App component so that both the SelectCountry and DisplayStatistics components can access and update the selected country code. By doing this, we ensure that the selected country code is consistent across both components and that changes to the state are reflected in both components.
A downside of lifting state up is that it can lead to more complex state management, especially as the application grows. A common issue is known as prop drilling, where you need to pass props through multiple layers of components to reach a deeply nested component. To address this issue, you can use state management libraries like Redux or context API to manage shared state more effectively. We will explore these concepts in future tutorials.
Conclusion
In this task, you implemented the Covid dashboard by adding state to the SelectCountry and DisplayStatistics components. You fetched the list of countries and the Covid data from the APIs. You passed the selected country code from the SelectCountry component to the DisplayStatistics component. You also learned about lifting state up in React and how to manage shared state between components.