useFetchFollowers

Overview

The useFetchFollowers hook fetches the list of users who are following the current logged-in user. It returns paginated results with detailed follow information including when each follow occurred.

Usage Example

import { useFetchFollowers } from "@replyke/react-js";
 
function MyFollowersList() {
  const fetchFollowers = useFetchFollowers();
 
  const loadFollowers = async () => {
    try {
      const result = await fetchFollowers({ page: 1, limit: 10 });
 
      console.log(`Found ${result.pagination.totalCount} total followers`);
      result.followers.forEach(follower => {
        console.log(`${follower.user.username} followed on ${follower.followedAt}`);
      });
    } catch (error) {
      console.error("Failed to fetch followers:", error.message);
    }
  };
 
  return <button onClick={loadFollowers}>Load My Followers</button>;
}

Advanced Usage with Pagination

import { useFetchFollowers, FollowersResponse } from "@replyke/react-js";
import { useState } from "react";
 
function PaginatedFollowersList() {
  const fetchFollowers = useFetchFollowers();
  const [followers, setFollowers] = useState<FollowersResponse | null>(null);
  const [loading, setLoading] = useState(false);
 
  const loadPage = async (page: number) => {
    setLoading(true);
    try {
      const result = await fetchFollowers({ page, limit: 20 });
      setFollowers(result);
    } catch (error) {
      console.error("Failed to load followers:", error.message);
    } finally {
      setLoading(false);
    }
  };
 
  return (
    <div>
      {followers?.followers.map(follower => (
        <div key={follower.followId}>
          {follower.user.username} - Followed: {new Date(follower.followedAt).toLocaleDateString()}
        </div>
      ))}
 
      {followers && (
        <div>
          Page {followers.pagination.currentPage} of {followers.pagination.totalPages}
          {followers.pagination.hasNextPage && (
            <button onClick={() => loadPage(followers.pagination.currentPage + 1)}>
              Next Page
            </button>
          )}
        </div>
      )}
    </div>
  );
}

Parameters & Returns

Parameters

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

ParameterTypeRequiredDefaultDescription
pagenumberNo1Page number for pagination
limitnumberNo20Number of followers to return per page

Returns

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

FollowersResponse

FieldTypeDescription
followersFollowerWithFollowInfo[]Array of follower information
paginationPaginationInfoPagination metadata

FollowerWithFollowInfo

FieldTypeDescription
followIdstringUnique ID of the follow relationship
userUserUser object containing follower information
followedAtstringISO date string of when the follow occurred

PaginationInfo

FieldTypeDescription
currentPagenumberCurrent page number
totalPagesnumberTotal number of pages available
totalCountnumberTotal number of followers
hasNextPagebooleanWhether there are more pages after current page
hasPreviousPagebooleanWhether there are pages before current page
limitnumberNumber of items per page used in this request

Error Handling

The hook will throw errors in the following cases:

  • No project is specified
  • No user is logged in