π οΈ 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
" alt="buggy nav bar">
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 |