• Custom React hook to manage and fetch multiple user rewards based on optional parameters like projectId and rewardId.

    This hook fetches user rewards data, handles caching, and provides methods for refreshing the data.

    Parameters

    • Optional params: {
          projectId?: string;
          rewardId?: string;
          [key: string]: any;
      } = {}

      Optional parameters used for fetching specific user rewards.

      • [key: string]: any
      • Optional projectId?: string

        If provided, fetches user rewards associated with a specific project.

      • Optional rewardId?: string

        If provided, fetches user rewards for a specific reward.

    Returns UseUserRewardsReturn

    The returning object contains:

    • refresh: FetchUserRewardsMethod - Function to refresh or re-fetch the user rewards data.
    • userRewards: The current list of user rewards.
    • fetched: Boolean indicating whether the user rewards data has been successfully fetched.

    Implementation Details

    • Caching: User rewards data is cached for 120 seconds to avoid redundant network requests.
    • Effect Trigger: The useEffect hook re-fetches user rewards whenever the params change.
    • Error Handling: Any errors during the fetch process are logged and re-thrown with additional context.
    • Refreshing Data: The refresh method can be used to manually trigger a re-fetch of the user rewards.

    Example

    import { useUserRewards } from 'react-playmakers';

    const UserRewardsComponent: React.FC = () => {
    const { userRewards, fetched, refresh } = useUserRewards({ projectId: 'pXXXXXXXX' });

    useEffect(() => {
    if (!fetched) {
    console.log("Fetching user rewards...");
    }
    }, [fetched]);

    const handleRefresh = async () => {
    await refresh();
    console.log("User rewards refreshed.");
    };

    return (
    <div>
    {fetched ? (
    <div>
    <h2>User Rewards</h2>
    <ul>
    {userRewards.map((reward, index) => (
    <li key={index}>{reward.name}</li>
    ))}
    </ul>
    </div>
    ) : (
    <p>Loading...</p>
    )}
    <button onClick={handleRefresh}>Refresh Rewards</button>
    </div>
    );
    };

Generated using TypeDoc