From NavBar Mess to React Router Success! Fixing Layouts, Routing & AIOHA in One Go ๐Ÿš€

in HiveDevs โ€ข 15 days ago

๐Ÿ› ๏ธ From NavBar Mess to React Router Success! ๐Ÿ˜‚

Hello Hive Community ๐Ÿ‘‹,


๐ŸŽฏ Todayโ€™s Mission (Impossible?)

  1. Fix the broken NavBar ๐Ÿ˜ต
  2. Integrate navigation using react-router-domโ€™s <Link>
  3. Create a layout to avoid repeating NavBar on every page

๐Ÿช› Fixing the NavBar Bug

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 โ˜•...

one little silly mistake

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

Regular Login Primary 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!

Daisy Documentation

Thank you DaisyUI for making this super easy. Just โœ‚๏ธ copy & paste...

Update NavBar


๐Ÿ”— Use React Routerโ€™s <Link> โ€” No More <a>

react router link

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


๐Ÿš€ My Contributions to โ™ฆ๏ธ Hive Ecosystem

ContributionToHiveEcosystem
Hive Witness NodeHive API Node (in progress)3Speak Video Encoder Node Operator (highest number of nodes)3Speak Mobile App Developer
3Speak Podcast App Developer3Speak Shorts App Developer3Speak Support & Maintenance TeamDistriator Developer
CheckinWithXYZHive InboxHiFindHive Donate App
Contributed to HiveAuth Mobile AppEcency โ†” 3Speak IntegrationEcency โ†” InLeo IntegrationEcency โ†” Actifit Integration
Hive Stats AppVote for Witness AppHiveFlutterKitNew 3Speak App

๐Ÿ™Œ Support Back

โค๏ธ Appreciate my work? Consider supporting @threespeak & @sagarkothari88! โค๏ธ