Sagar Kothari avatar

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

sagarkothari88

Published: 01 Aug 2025 β€Ί Updated: 01 Aug 2025Day 6 πŸš€ - Learning ReactJS: Calling APIs & Creating Context Providers | Distriator Feature Update

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

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


πŸš€ 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@threespeak & sagarkothari88@sagarkothari88! ❀️

VoteForWitness
sagarkothari88sagarkothari88@sagarkothari88
threespeakthreespeak@threespeak

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

Written by

App Developer on Hive - Distriator, Check in With XYZ, 3Speak, HiveSuite.app.

Read more #hive posts


Best Posts From Sagar Kothari

We have not curated any of sagarkothari88's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Sagar Kothari