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@latestto 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)