React & React NativeHooksFollowsuseFetchFollowingCountByUserId

useFetchFollowingCountByUserId

Overview

The useFetchFollowingCountByUserId hook fetches the total number of users that a specific user is following. This is a public endpoint that allows you to retrieve following counts for any user without authentication.

Usage Example

import { useFetchFollowingCountByUserId } from "@replyke/react-js";
 
function UserFollowingCounter({ targetUserId }: { targetUserId: string }) {
  const fetchFollowingCountByUserId = useFetchFollowingCountByUserId();
 
  const getUserFollowingCount = async () => {
    try {
      const result = await fetchFollowingCountByUserId({ userId: targetUserId });
      console.log(`This user is following ${result.count} people`);
    } catch (error) {
      console.error("Failed to fetch user following count:", error.message);
    }
  };
 
  return <button onClick={getUserFollowingCount}>Check User's Following</button>;
}

Advanced Usage with State

import { useFetchFollowingCountByUserId } from "@replyke/react-js";
import { useState, useEffect } from "react";
 
function UserProfileStats({ userId, username }: { userId: string; username: string }) {
  const fetchFollowingCountByUserId = useFetchFollowingCountByUserId();
  const [followingCount, setFollowingCount] = useState<number | null>(null);
  const [loading, setLoading] = useState(false);
 
  useEffect(() => {
    if (!userId) return;
 
    const loadCount = async () => {
      setLoading(true);
      try {
        const result = await fetchFollowingCountByUserId({ userId });
        setFollowingCount(result.count);
      } catch (error) {
        console.error("Failed to load following count:", error);
        setFollowingCount(0);
      } finally {
        setLoading(false);
      }
    };
 
    loadCount();
  }, [userId, fetchFollowingCountByUserId]);
 
  return (
    <div className="user-profile-stats">
      <h3>{username}</h3>
      <div className="stats">
        {loading ? (
          <span>Loading...</span>
        ) : (
          <>
            <span className="count">{followingCount?.toLocaleString() ?? 0}</span>
            <span className="label">Following</span>
          </>
        )}
      </div>
    </div>
  );
}

Parameters & Returns

Parameters

The hook returns a function that accepts an object with the following field:

ParameterTypeRequiredDescription
userIdstringYesThe ID of the user whose following count to fetch

Returns

The function returns a Promise that resolves to an object containing:

FieldTypeDescription
countnumberThe total number of users this user is following

Error Handling

The hook will throw errors in the following cases:

  • No user ID is provided
  • No project is specified

Notes

This is a public endpoint that doesn’t require user authentication, making it suitable for displaying any user’s following count in public-facing features like user profiles or discovery pages.