Hey there, Hive community! ๐
Welcome back to my wild ReactJS learning ride ๐ข โ and guess what? Weโre already on Day 6! If you've missed previous episodes, don't worry. Grab some popcorn ๐ฟ and catch up below:
๐ ReactJS Journey So Far
- ๐ 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 ๐
- ๐งน Day Five: Path Aliases to clean up relative path spaghetti ๐
๐ก API Calling โ The Right Way
Using fetch()
is easy. But keeping things organized? Thatโs next-level!
๐ Folder structure:
src/api/
โโโ business/
โ โโโ BusinessApi.tsx // ๐ Get Business API here
๐ Hereโs how I call my Business API:
import type { BusinessDTO } from "@/types/BusinessApiResponse";
export const fetchBusinesses = async () => {
const response = await fetch("https://some-xyz-server.com/business");
if (!response.ok) throw new Error("API error");
const jsonData = await response.json();
return jsonData as BusinessDTO[];
};
๐ก Why a separate file? Because real-world APIs are messy. Headers, body payloads, response transformations โ it's cleaner this way! ๐
๐งฌ DTOs / POJOs / Data Models
Using TypeScript = getting predictable & strongly typed data ๐ช
๐ Types live here:
src/types/
โโโ BusinessApiResponse.tsx // ๐ All your DTOs in one place
Hereโs a sneak peek of my DTO setup:
export interface BusinessDTO {
distriator?: DistriatorDTO;
profile?: ProfileDTO;
contact?: ContactDTO;
location?: LocationDTO;
id?: string;
}
export interface ContactDTO {
website?: string;
}
export interface DistriatorDTO {
guides?: GuideDTO[];
owner?: string;
creator?: string;
expiry?: Date;
subscriptionStatus?: string;
paymentMethods?: string[];
spendHBDLink?: string;
}
๐ง Context Providers to the Rescue!
If businesses are needed everywhere in your app, the answer is simple: Context Provider! ๐ช
โ Step 1: Folder Setup
src/
โโโ components/
โ โโโ BusinessList.jsx
โโโ context/
โ โโโ BusinessesContext.jsx // โ
Lives here
โ Step 2: Create the Context + Provider
import type { FC, ReactNode } from "react";
import type { BusinessDTO } from "@/types/BusinessApiResponse";
import { fetchBusinesses } from "@/api/BusinessApi";
import React, { createContext, useContext, useState, useEffect } from "react";
const BusinessesContext = createContext();
export const useBusinesses = () => useContext(BusinessesContext);
export const BusinessesProvider: FC<{ children: ReactNode }> = ({ children }) => {
const [businesses, setBusinesses] = useState<BusinessDTO[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
refreshBusinesses();
}, []);
const refreshBusinesses = async () => {
try {
setLoading(true);
const data = await fetchBusinesses();
setBusinesses(data);
} catch (e) {
console.error(e);
} finally {
setLoading(false);
}
};
return (
<BusinessesContext.Provider
value={{ businesses, loading, refreshBusinesses }}
>
{children}
</BusinessesContext.Provider>
);
};
โ Step 3: Wrap Your App
import { BusinessesProvider } from "@/context/BusinessesContext";
function App() {
return (
<BusinessesProvider>
<AiohaProvider aioha={aioha}>
<BrowserRouter>
<Routes>
<Route path="/" element={<LandingPage />} />
</Routes>
</BrowserRouter>
</AiohaProvider>
</BusinessesProvider>
);
}
โ Step 4: Use It Anywhere!
import React from "react";
import { useBusinesses } from "@/context/BusinessesContext";
function BusinessList() {
const { businesses, loading } = useBusinesses();
if (loading) return <p>โณ Loading businesses...</p>;
return (
<div>
<h2>๏ฟฝ๏ฟฝ All Businesses</h2>
<ul>
{businesses.map((biz) => (
<li key={biz.id}>๏ฟฝ๏ฟฝ {biz.name}</li>
))}
</ul>
</div>
);
}
export default BusinessList;
And boom ๐ฅ โ your business data is now globally available!
๐ง Distriator Project Update
Weโve rolled out a new feature that displays Hive Power of a business! ๐
This allows users to instantly see how invested a business is in Hive ๐ช
โ
Currently in alpha
๐ Rolling out to production very soon!
๐ Wrapping Up
I hope this post helps some curious mind someday ๐ง
If you found value in this, consider supporting me in my journey on the Hive blockchain ๐
๐ Vote me as a Hive Witness
โก Letโs build a stronger decentralized world together
Cheers ๐ฅ
More power to Hive community members!
More power to Hive blockchain!
๐ 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/688ad30f-5d64-8000-9f97-b26950da7bd6
๐ 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 |