@hateoas-ts/resource-react - v1.4.0
    Preparing search index...

    Function useResource

    • Hook for fetching and managing a single HATEOAS resource.

      Automatically fetches the resource on mount and returns loading/error states along with the resource data. Use this hook when you need to display a single entity (not a collection).

      Type Parameters

      • T extends Entity<any, Record<string, any>>

      Parameters

      • resourceLike: ResourceLike<T>

        A Resource, ResourceRelation, or URI string

      Returns UseResourceResponse<T>

      Loading state, error, and resource data

      import { useResource, useClient } from '@hateoas-ts/resource-react';
      import type { User } from './types';

      function UserProfile({ userId }: { userId: string }) {
      const client = useClient();
      const userResource = client.go<User>(`/api/users/${userId}`);

      const { loading, error, data, resourceState } = useResource(userResource);

      if (loading) return <div>Loading...</div>;
      if (error) return <div>Error: {error.message}</div>;

      return <div>Welcome, {data.name}!</div>;
      }
      // Follow a HATEOAS link to get a related resource
      const { data: profile } = useResource(
      userResource.follow('profile')
      );