Function useUser

  • Custom React hook for fetching and managing user data.

    Parameters

    • userId: string

      The ID of the user to fetch.

    Returns UseUserReturn

    The returning object contains:

    • refresh: FetchUserMethod - Function to refresh the user data.
    • fetched: A boolean flag indicating whether the user data has been fetched.
    • user: The fetched user object or null if not yet available.

    Implementation Details

    • Caching: API responses are cached for for 60 seconds (60,000 ms) to minimize redundant network requests.
    • Effect Trigger: The useEffect hook is used to initiate the data fetch when the userId changes.
    • Error Handling: Any errors encountered during the fetch process are logged to the console and re-thrown with additional context for clarity.

    Example

    Example using the useUser hook within a component with a test user ID to fetch and display user data.

    import { useUser } from 'react-playmakers';

    export default function UserComponent() {

    // calling with test user ID
    const { user } = useUser("50ecc99c-f0d1-7003-0cfe-b0be62e6363f");

    // Display the user's username if available
    return (
    <div>
    <h1>Welcome, {user?.username}</h1>
    </div>
    );
    }

Generated using TypeDoc