Why You Should Create a Template
Creating new React projects can be both time-consuming and redundant. In this guide, Iâll show you how to set up a reusable React template with Vite and Tailwind CSS. This approach allows you to skip the initial set-up process and jump straight into development for future projects.
If youâre looking for a quick start, Iâve created a ready-to-use template on GitLab here. Just clone it to your machine, and youâll be up and running.
Now, letâs get started by setting up a remote repository.
Setting Up a Remote Repository
- Sign In to the Version Control Platform of Your Choice - Iâll be using GitLab, but any other choice works
-
Create a New Project - In GitLab, this can be done by clicking the â+â icon on the upper left corner by your profile picture
- Select âCreate blank projectâ
- Choose a project name, slug, + privacy level
- Hit the âCreate Projectâ Button
- Under the Blue âCodeâ Button, copy the âClone with HTTPSâ link - Weâll need this soon, so just hold onto it
And youâre all set! Now your template will be automatically linked to this remote repo when you clone it.
Initializing the Template
Letâs explore the first steps to creating your template
-
Navigate to the Folder Where You Want to Store the Template:
- Open your terminal
-
Change into your desired directory
e.g.
cd my-projects
-
Clone Your Template:
- Replace with the one you copied in the previous section
- Change into your new templateâs directory
git clone <Remote-Clone-Link> cd <new-template-name>
Initialize a Vite Project:
-
Use â.â after
vite@latest
to prevent Vite from creating an extra folder inside the cloned git repo
npm create vite@latest .
Select âReactâ and âTypeScriptâ when Vite prompts you for project options
đĄ Note: At this point, I like to remove a lot of the default Vite files + assets
Installing Packages
Feel free to customize this section. Hereâs my preferred package combination:
- tailwindcss - a utility-first CSS framework
- postcss - process and optimizes CSS files
- autoprefixer - adds prefixes to ensure CSS compatibility across all browsers
- prettier - auto formats your code to improve readability
- prettier-plugin-tailwindcss - ensures consisten Tailwind class formatting (personal favorite)
npm install -D tailwindcss postcss autoprefixer prettier prettier-plugin-tailwindcss
Configuring Tailwind and Prettier
-
tailwind.config.js
- content: specifies which file types should use Tailwind classes
- theme.extend: allows you to create custom classes
// -- tailwind.config.js --
export default {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
- index.css - imports Tailwindâs base styles, components, and utility classes
// -- index.css --
@tailwind base;
@tailwind components;
@tailwind utilities;
- .prettierrc - allows Prettier to format Tailwind classes
// -- .prettierrc
{
"plugins": ["prettier-plugin-tailwindcss"]
}
Committing and Pushing to the Remote Repo
Youâre almost finished! To make this truly reusable, we need to save our changes to our remote repo
git add .
git commit -m"Finish react template"
git push
Final Notes
- Update Packages Regularly - Always check in on your project to ensure packages arenât deprecated and that updates donât break your project
- Remove the .git File When Cloning - This ensures that your changes to your new project donât affect the templateâs remote repo
Conclusion
Congratulations, by creating a React project template, youâve significantly streamlined the setup process, saving yourself valuable time. Now, you can focus on actually building out your application and bringing your idea to life.
What did you think? Iâm always open to feedback and suggestions, so feel free to share your thoughts or ask questions!
Top comments (0)