Day 6 πŸš€ - Learning ReactJS: Calling APIs & Creating Context Providers | Distriator Feature Update

@sagarkothari88 Β· 2025-08-01 04:31 Β· HiveDevs

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


πŸ“‘ API Calling β€” The Right Way

Fetch Data from server

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

Type Script - Type Image

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!

Context Provider Example Image

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 πŸ’ͺ

businesses with hive power

βœ… 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

#hive #development #webapp #reactjs #javascript #frontend #webdev #india #waiv #neoxian
Payout: 0.000 HBD
Votes: 130
More interactions (upvote, reblog, reply) coming soon.