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 π
π Catch Up on My React Adventures
- π Day One: Fresh React app + AIOHA integration!
- π§ Day Two: Routing drama & rebellious NavBar π€
- π οΈ Day Three: Fixed Layouts, Routing & AIOHA π₯
- π§ Day Four: useState, useEffect, and .env headaches π
- π₯ Today is Day Five: Letβs clean up those imports with path aliases!
π Why Path Aliases?
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 π
πͺ Step-by-Step: Setting Up Path Aliases
π§ Step 1: Update tsconfig.app.json
Add these compiler options to set your base path and alias:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
// ...other stuff
}
}
βοΈ Step 2: Tweak vite.config.ts
Use 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.
π Step 3: Restart Everything!
- Stop your
npm run dev
server. - Restart TypeScript in VS Code (click on the TS version and hit "Restart TS Server").
- Before starting again... do not skip step 4 π€£
βοΈ Step 4: Update Your Imports!
Time to ditch the dot-dot-dot πͺ
Use beautiful imports like:
import Button from "@/components/ui/Button"
Isnβt that satisfying? π
π Sources That Helped (But Also Confused Me π)
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 π»πͺ
π Final Note
- I asked ChatGPT/AI to help optimize this post to make it more readable and viewer-friendly.
- Here is the link where you can find both original content & improvements made by AI
- https://chatgpt.com/share/68897d05-0eb0-8000-8f07-77280f4f2375
π My Contributions to β¦οΈ Hive Ecosystem
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 |
π Support Back
β€οΈ Appreciate my work? Consider supporting @threespeak & @sagarkothari88! β€οΈ
Vote | For | Witness |
---|---|---|
sagarkothari88 | @sagarkothari88 | |
threespeak | @threespeak |
Comments (3)
Stop downvoting my original content with your alt account @letusbuyhive
You have been exposed ππππππ
This post has been manually curated by @bhattg from Indiaunited community. Join us on our Discord Server.
Do you know that you can earn a passive income by delegating to @indiaunited. We share more than 100 % of the curation rewards with the delegators in the form of IUC tokens. HP delegators and IUC token holders also get upto 20% additional vote weight.
Here are some handy links for delegations: 100HP, 250HP, 500HP, 1000HP.
100% of the rewards from this comment goes to the curator for their manual curation efforts. Please encourage the curator @bhattg by upvoting this comment and support the community by voting the posts made by @indiaunited.
Thank you so much @indiaunited & @bhattg for curation.
via Inbox
Path aliases are a total game-changer! Those ../../../../ imports were a nightmare. When I set up my React-Vite project, the first thing I configure is the @ alias.
Thank you @devmamun
I feel happy that it helped you
via Inbox