In this task, you will create the layout for the Covid Dashboard App using the shadcn/ui component library. The layout will consist of a header with a select dropdown to choose a country and a statistics section displaying the Covid-19 statistics for the selected country.
Step 1: Install Shadcn UI
The component library we will use is Shadcn/UI. This is not a typical component library; it’s a collection of reusable components that you can copy and paste into your applications. To achieve this goal, we will use the shadcn-cli to install the components. However, you must first configure your project to enable the shadcn-cli to work with it.
Resolve paths
To resolve paths for the Shadcn/UI component library, add the following code to all three tsconfig.app.json, tsconfig.node.json, and tsconfig.json files:
Moreover, update vite.config.ts so your app can resolve paths without error:
Since we import the “path” module, we need to install the types for Node.js.
Now we have properly configured our project to map the @ alias to the src directory.
Use the Shadcn CLI
In this step, we will use the shadcn CLI to create a components.json file that will configure the Shadcn/UI components for our project.
First, open the terminal and run the shadcn init command as shown below:
You will be asked a few questions to configure components.json:
After answering the questions, you will see the following output:
Now the components.json file is created in the root of your project. The file will look like this:
Add components
You can now start adding components to your project. This is facilitated by the shadcn-cli. For example, to add a button component, run the following command:
This will add a button.tsx file to the src/components/ui directory. You can now use this component in your application. Update the src/App.tsx file to use the button component:
Now you can run your application (using pnpm dev), navigate to the browser, and you should be able to see the button component in action:
Step 2: Create the layout
In this step, we will create the layout for the Covid Dashboard App. The layout will consist of a header with a select dropdown to choose a country and a statistics section displaying the Covid-19 statistics for the selected country.
We will need the following components for our layout. Run the following commands to add these components:
We don’t need the button component anymore, so we can remove it:
Next, update the src/App.tsx file to create the layout:
Now you can run your application (using pnpm dev), navigate to the browser, and you should be able to see the layout for the Covid Dashboard App:
Step 3: Break the layout into components
In this step, we will break the layout into components. We will create two components: SelectCountry and DisplayStatistics.
The SelectCountry component
Create a new file src/components/select-country.tsx:
The DisplayStatistics component
Create a new file src/components/display-statistics.tsx:
Update the App component
Update src/App.tsx to use the new components:
Notice the JSX syntax to import the components. Although components are functions, we use the component name as if it were a tag.
The advantage of breaking the layout into components is that it makes the code more readable and maintainable. Each component is responsible for a specific part of the layout, making it easier to understand and modify.
Thinking in Components
When building a React application, it’s essential to think in components. Components are the building blocks of a React application. They are reusable, independent, and encapsulate the logic and UI of a specific part of the application.
React components are JavaScript functions (or classes) that describe a part of the UI and return HTML (or JSX) to be rendered on a web page. Components can be nested inside other components, allowing you to create complex UIs from simple building blocks. By breaking down the UI into components, we can achieve several benefits:
Reusability: Components can be reused across different parts of the application, reducing code duplication and making the codebase more maintainable.
Separation of Concerns: Components encapsulate both the UI and the logic related to that UI, making it easier to reason about and maintain the code.
Composition: Components can be composed together to create more complex UIs. By combining simple components, you can build more sophisticated interfaces.
Isolation: Components are isolated from each other, meaning that changes to one component do not affect other components. This makes it easier to test and debug the application.
By thinking in components, you can break down the UI into smaller, more manageable pieces, making it easier to build and maintain React applications.
Conclusion
In this task, you learned how to create the layout for the Covid Dashboard App using the Shadcn/UI component library. You installed the Shadcn/UI components using the shadcn-cli and created the layout for the app. You then broke the layout into components to make the code more readable and maintainable. By thinking in components, you can build complex UIs from simple building blocks, making it easier to develop and maintain React applications.
In the next task, you will fetch the Covid-19 statistics for the selected country and display them in the app.