Skip to main content

useFollowManager

Overview

The useFollowManager hook is a comprehensive follow state management solution that combines follow status checking, following, and unfollowing functionality into a single, easy-to-use interface. It automatically manages loading states and provides a toggle function for seamless follow/unfollow interactions.

Usage Example

Advanced Usage with Complete UI State

Usage in User Profile Card

Usage with Follow Count Display

Parameters & Returns

Parameters

The hook accepts an object with the following field:
userId
string
required
The ID of the user to manage follow status for

Returns

The hook returns an object containing:
isFollowing
boolean \
null`
isLoading
boolean
Whether the follow status is being loaded
toggleFollow
() => Promise<void>
Function to toggle the follow status

States

isFollowing

  • null - Follow status is being loaded or user is not logged in
  • true - Currently following the user
  • false - Not following the user

isLoading

  • true - Initial follow status is being fetched
  • false - Follow status has been loaded

Error Handling

The hook handles errors internally for status fetching, but the toggleFollow function can throw errors that should be caught by the consuming component. Common error scenarios:
  • Network issues during follow/unfollow operations
  • Authentication problems
  • User attempting to follow themselves
  • Server-side validation errors

Automatic Behavior

The hook automatically:
  • Fetches initial follow status when the component mounts
  • Prevents actions when the user ID matches the current user’s ID
  • Manages optimistic UI updates during toggle operations
  • Handles loading states for both initial load and toggle operations

Use Cases

This hook is ideal for:
  • User profile pages with follow/unfollow buttons
  • User discovery interfaces
  • Social feed user cards
  • User search results
  • Follow recommendations
  • Bulk follow management interfaces
  • useFollowUser - Basic follow functionality
  • useUnfollowUserByUserId - Basic unfollow functionality
  • useFetchFollowStatus - Follow status checking only
  • useConnectionManager - Similar management hook for connections