Hello Hive Community Members! 👋
Hope you're all doing fantastic! I'm back with Day 5 of my ReactJS learning series 🚀
This journey has been quite the rollercoaster, and today we're diving into something every dev struggles with — those ugly relative imports 😩
But before that, if you missed my earlier episodes, here's your cheat sheet 👇
Let’s face it —
import Button from "../../../../components/ui/Button"
is just plain ugly and hard to maintain 😵💫
While pairing up with another dev (shoutout to AI-generated code ✨), I noticed a cleaner import style — so I had to figure it out myself! Here’s how you can too 👇
tsconfig.app.jsonAdd these compiler options to set your base path and alias:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
// ...other stuff
}
}
vite.config.tsUse path module to create an alias for @ pointing to your src folder.
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import path from "path";
// https://vite.dev/config/
export default defineConfig({
plugins: [
react(),
tailwindcss()
],
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
},
},
});
🎉 Boom! You’re 90% done.
npm run dev server.Time to ditch the dot-dot-dot 🔪
Use beautiful imports like:
import Button from "@/components/ui/Button"
Isn’t that satisfying? 😍
Honestly... I followed these and ended up with TypeScript errors. So, the method described in this post is what actually worked for me! 🙌
Thanks for following along!
See you in the next post where I’ll probably break something else and then fix it (again) 😅
Stay curious & keep coding 💻💪
| Contribution | To | Hive | Ecosystem |
|---|---|---|---|
| Hive Witness Node | Hive API Node (in progress) | 3Speak Video Encoder Node Operator (highest number of nodes) | 3Speak Mobile App Developer |
| 3Speak Podcast App Developer | 3Speak Shorts App Developer | 3Speak Support & Maintenance Team | Distriator Developer |
| CheckinWithXYZ | Hive Inbox | HiFind | Hive Donate App |
| Contributed to HiveAuth Mobile App | Ecency ↔ 3Speak Integration | Ecency ↔ InLeo Integration | Ecency ↔ Actifit Integration |
| Hive Stats App | Vote for Witness App | HiveFlutterKit | New 3Speak App |
❤️ Appreciate my work? Consider supporting &
! ❤️
| Vote | For | Witness |
|---|---|---|
| sagarkothari88 | ||
| threespeak |