useFetchFollowing

Overview

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

Usage Example

import { useFetchFollowing } from "@replyke/react-js";
 
function MyFollowingList() {
  const fetchFollowing = useFetchFollowing();
 
  const loadFollowing = async () => {
    try {
      const result = await fetchFollowing({ page: 1, limit: 10 });
 
      console.log(`Following ${result.pagination.totalCount} users`);
      result.following.forEach(follow => {
        console.log(`Following ${follow.user.username} since ${follow.followedAt}`);
      });
    } catch (error) {
      console.error("Failed to fetch following:", error.message);
    }
  };
 
  return <button onClick={loadFollowing}>Load Who I'm Following</button>;
}

Advanced Usage with Pagination

import { useFetchFollowing, FollowingResponse } from "@replyke/react-js";
import { useState } from "react";
 
function PaginatedFollowingList() {
  const fetchFollowing = useFetchFollowing();
  const [following, setFollowing] = useState<FollowingResponse | null>(null);
  const [loading, setLoading] = useState(false);
 
  const loadPage = async (page: number) => {
    setLoading(true);
    try {
      const result = await fetchFollowing({ page, limit: 15 });
      setFollowing(result);
    } catch (error) {
      console.error("Failed to load following:", error.message);
    } finally {
      setLoading(false);
    }
  };
 
  return (
    <div>
      {following?.following.map(follow => (
        <div key={follow.followId}>
          {follow.user.username} - Following since: {new Date(follow.followedAt).toLocaleDateString()}
        </div>
      ))}
 
      {following && (
        <div>
          Page {following.pagination.currentPage} of {following.pagination.totalPages}
          {following.pagination.hasNextPage && (
            <button onClick={() => loadPage(following.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 following users to return per page

Returns

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

FollowingResponse

FieldTypeDescription
followingFollowingWithFollowInfo[]Array of following information
paginationPaginationInfoPagination metadata

FollowingWithFollowInfo

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

PaginationInfo

FieldTypeDescription
currentPagenumberCurrent page number
totalPagesnumberTotal number of pages available
totalCountnumberTotal number of users you’re following
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