In this task, you will set up a new React project using Vite. You will also clean up the boilerplate code and add additional dependencies such as Tailwind CSS and Prettier.
Step 1: Create a new Vite project
Run the following command to create a new Vite project with the React template:
Next, navigate to the project directory and open it in Visual Studio Code:
Notice that the Vite project has been created with the following files and directories:
Step 2: Clean up the boilerplate code
In this step, we will clean up the boilerplate code generated by Vite. We will remove unnecessary files and directories and update the content of the remaining files.
First, remove the following files and directories:
Next, create a new file /public/covid.svg and paste the following content:
Now open the index.html file and replace the content with the following:
Next, open /src/App.tsx and replace the content with the following:
Finally, open README.md and replace the content with the following:
It is time to test the application. Run the following commands:
Navigate to http://localhost:5173 in your browser to see the (empty) Covid Dashboard.
Step 3: Install Tailwind CSS
In this step, we will install Tailwind CSS and its dependencies. First, install Tailwind CSS, PostCSS, and Autoprefixer as development dependencies:
Next, generate a tailwind.config.js file and a postcss.config.js file by running the following command:
Update the tailwind.config.js file to include the paths to all of your template files:
Next, add the @tailwind directives for each of Tailwind’s layers to your ./src/index.css file:
Finally, to test that Tailwind CSS is working, update the ./src/App.tsx file to include the following:
Run the app again and navigate to http://localhost:5173 in your browser to see the Covid Dashboard with Tailwind CSS applied.
Step 4: Install Prettier
In this step we will install Prettier as a development dependency.
Then, we will create a .prettierrc.json file in the root of our project to configure Prettier.
Next, create a .prettierignore file to let Prettier know which files to ignore.
Finally, add this command to the scripts section of the package.json file:
Run the following command to format your code with Prettier:
Make ESlint and Prettier work together
Recall that we installed Eslint when we created the Vite project. Now we will install the eslint-plugin-prettier and eslint-config-prettier packages to make Eslint and Prettier work together.
Add “prettier” to the “extends” array in your .eslintrc.cjs file. Make sure to put it last, so it gets the chance to override other configs.
Moreover, update ignorePatterns (in the same .eslintrc.cjs file) to include the following:
Update the lint script in the package.json file as follows:
Finally, run the following commands to format your code with Prettier and lint your code with Eslint:
Step 5: GitHub Action to deploy the app
If you haven’t already done so, initialize a local Git repository and commit your code.
We will deploy this app to GitHub pages. Create a GitHub repository and push your local code to it. Next, on your local repository, update the vite.config.ts file to include the base property:
Next, create a .github folder. Add a subfolder workflows to .github. Finally, add a deploy.yml file to this subfolder with the following content:
Before pushing these changes to GitHub, update the settings of your GitHub repository to use “GitHub Action” for deploying the app to GitHub Pages.
Push your code to the GitHub repository. This will trigger a build process that will result in deploying your app. From this point on, every time you make changes to your code on the main branch and push those changes to GitHub, the new app will be automatically deployed.
Conclusion
In this task, you have set up a new React project using Vite. You have cleaned up the boilerplate code, added Tailwind CSS and Prettier, and deployed the app to GitHub Pages using GitHub Actions. You are now ready to start building the Covid Dashboard app.
In the next task, you will explore the relevant APIs to fetch Covid-19 statistics for different countries.