๐ ๏ธ From NavBar Mess to React Router Success! ๐
Hello Hive Community ๐,
- ๐ Day before yesterday, we built a fresh React app and integrated AIOHA.
- ๐งญ Yesterday, we explored routing... but yeah, our NavBar decided to rebel. ๐ค
๐ฏ Todayโs Mission (Impossible?)
- Fix the broken NavBar ๐ต
- Integrate navigation using
react-router-dom
โs<Link>
- Create a layout to avoid repeating NavBar on every page
๐ช Fixing the NavBar Bug
See this monstrosity above? ๐ฑ Thatโs what we got even though we copied the beautiful DaisyUI navbar. ๐ค
After a LOT of head-scratching and a few cups of chai โ...
Boom! Found it โ I forgot to import daisyui
in app.css
๐คฆ
One small import for me, one giant leap for NavBar!
๐งฉ Updated NavBar Component
import React from "react";
import HiveUserAvatarButton from "./HiveUserAvatarButton";
const NavBar = () => {
return (
<div className="navbar bg-base-100 shadow-sm">
<div className="flex-1">
<a className="btn btn-ghost text-xl">Distriator</a>
</div>
<div className="flex-none">
<HiveUserAvatarButton />
</div>
</div>
);
};
export default NavBar;
Notice weโre using a fancy new
HiveUserAvatarButton
component ๐
๐งโ๐ Custom AIOHA Login Button
Time to ditch that boring default button.
Letโs use our custom-built login avatar button that:
- Shows a Login button if user is not logged in
- Displays avatar if logged in โ
import { useState } from "react";
import { useAioha, AiohaModal } from "@aioha/react-ui";
import { KeyTypes } from "@aioha/aioha";
import "@aioha/react-ui/dist/build.css";
const HiveUserAvatarButton = () => {
const [modalDisplayed, setModalDisplayed] = useState(false);
const { user } = useAioha();
function userAvatarBasedButton() {
return (
<div className="avatar" onClick={() => setModalDisplayed(true)}>
<div className="w-10 rounded-full overflow-hidden">
<img src={`https://images.hive.blog/u/${user}/avatar`} />
</div>
</div>
);
}
function loginButton() {
return (<button className="btn btn-primary" onClick={() => setModalDisplayed(true)}>Login</button>);
}
function aiohaModel() {
return (
<div>
<AiohaModal
displayed={modalDisplayed}
loginOptions={{
msg: "Login",
keyType: KeyTypes.Posting,
}}
onLogin={console.log}
onClose={setModalDisplayed}
/>
</div>
);
}
return (
<>
{user ? userAvatarBasedButton() : loginButton()}
{aiohaModel()}
</>
);
};
export default HiveUserAvatarButton;
๐งฑ Letโs Build a Common Layout
Because... DRY (Donโt Repeat Yourself) ๐
import React from 'react'
import NavBar from './NavBar'
const CommonLayout = ({children}) => {
return (
<>
<div>
<NavBar />
</div>
<main>{children}</main>
</>
);
}
export default CommonLayout;
๐ Now every page can reuse this layout with the NavBar already baked in.
๐ Using the Common Layout on a Page
import React from 'react'
import CommonLayout from '../../Components/CommonLayout'
const AboutUsPage = () => {
return (
<CommonLayout>
<div>AboutUsPage</div>
</CommonLayout>
)
}
export default AboutUsPage;
๐งญ Add Nav Links for Navigation
Time to give our NavBar a promotion by adding About Us & Contact Us links!
Thank you DaisyUI for making this super easy. Just โ๏ธ copy & paste...
๐ Use React Routerโs <Link>
โ No More <a>
Replace all old-school <a href>
with <Link to>
from react-router-dom
โ
That way, no page refreshes and smooth routing FTW โก
๐งโ๐ป Final Words
Todayโs work may look small but helped build strong foundation for the app.
- โ NavBar fixed
- โ AIOHA integrated smartly
- โ Routing UX improved
- โ Codebase looks cleaner
Thanks for following along! See you in the next post ๐
Happy Building! ๐ปโค๏ธ
๐ 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/688771b9-0c28-8000-aef5-24976fe84278
๐ 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 |