reactive/data-client / .agents/skills/data-client-react
data-client-react
UnverifiedRead, change and render async and shared state in React with @data-client/react: any data from an API or other async source, and global state shared across components (it has to persist somewhere, which makes it async). Use it for loading and error states, mutations and forms, filtered or derived views, and debugging data that looks wrong or stale on screen. Use it in place of fetch in useEffect, or app-wide useState and context; its main hooks are useSuspense, useQuery and useController.
references/useDLE.md
View on GitHub<!-- Generated by `yarn build:skills` from docs/core/api/useDLE.md (react). Edit the source doc, not this file. -->
# useDLE() - \[D]ata \[L]oading \[E]rror
High performance async data rendering without overfetching. With fetch meta data.
In case you cannot use [suspense](./data-dependency.md#async-fallbacks), useDLE() is just like [useSuspense()](./useSuspense.md) but returns \[D]ata \[L]oading \[E]rror values.
`useDLE()` is reactive to data [mutations](./mutations.md); rerendering only when necessary.
## Usage
```typescript title="ProfileResource"
import { Entity, resource } from '@data-client/rest';
export class Profile extends Entity {
id: number | undefined = undefined;
avatar = '';
fullName = '';
bio = '';
static key = 'Profile';
}
export const ProfileResource = resource({
path: '/profiles/:id',
schema: Profile,
});
```
```tsx title="ProfileList" {6}
import React from 'react';
import { useDLE } from '@data-client/react';
import { ProfileResource } from './ProfileResource';
function ProfileList(): React.JSX.Element {
const { data, loading, error } = useDLE(ProfileResource.getList);
if (error) return <div>Error {`${error.status}`}</div>;
if (loading || !data) return <Loading />;
return (
<div>
{data.map(profile => (
<div className="listItem" key={profile.pk()}>
<Avatar src={profile.avatar} />
<div>
<h4>{profile.fullName}</h4>
<p>{profile.bio}</p>
</div>
</div>
))}
</div>
);
}
render(<ProfileList />);
```
## Behavior
| Expiry Status | Fetch | Data | Loading | Error | Conditions |
| ------------- | --------------- | ------------ | ------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Invalid | yes<sup>1</sup> | `undefined` | true | false | not in store, [deletion](https://dataclient.io/rest/api/resource#delete), [invalidation](./Controller.md#invalidate), [invalidIfStale](https://dataclient.io/docs/concepts/expiry-policy#endpointinvalidifstale) |
| Stale | yes<sup>1</sup> | denormalized | false | false | (first-render, arg change) & [expiry < now](https://dataclient.io/docs/concepts/expiry-policy) |
| Valid | no | denormalized | false | maybe<sup>2</sup> | fetch completion |
| | no | `undefined` | false | false | `null` used as second argument |
> **Note**
>
> 1. Identical fetches are automatically deduplicated
> 2. [Hard errors](https://dataclient.io/docs/concepts/error-policy#hard) to be [caught](./data-dependency.md#async-fallbacks) by [Error Boundaries](./AsyncBoundary.md)
> **Info: React Native**
>
> When using React Navigation, useDLE() will trigger fetches on focus if the data is considered
> stale.
> **Tip: Conditional Dependencies**
>
> Use `null` as the second argument to any Data Client hook means "do nothing."
> A [RestEndpoint](https://dataclient.io/rest/api/RestEndpoint) without path or search params takes `{}` in its place, like `enabled ? {} : null`.
>
> ```typescript
> // todo could be undefined if id is undefined
> const todo = useDLE(TodoResource.get, id ? { id } : null);
> ```
## Types
```typescript
function useDLE(
endpoint: ReadEndpoint,
...args: Parameters<typeof endpoint> | [null]
): {
data: Denormalize<typeof endpoint.schema>;
loading: boolean;
error: Error | undefined;
};
```
```typescript
function useDLE<
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(
endpoint: E,
...args: Args
): {
data: DenormalizeNullable<typeof endpoint.schema>;
loading: boolean;
error: Error | undefined;
};
```
## Examples
### Detail
```typescript title="ProfileResource"
import { Entity, resource } from '@data-client/rest';
export class Profile extends Entity {
id: number | undefined = undefined;
avatar = '';
fullName = '';
bio = '';
static key = 'Profile';
}
export const ProfileResource = resource({
path: '/profiles/:id',
schema: Profile,
});
```
```tsx title="ProfileDetail" {6-10}
import React from 'react';
import { useDLE } from '@data-client/react';
import { ProfileResource } from './ProfileResource';
function ProfileDetail(): React.JSX.Element {
const {
data: profile,
loading,
error,
} = useDLE(ProfileResource.get, { id: 1 });
if (error) return <div>Error {`${error.status}`}</div>;
if (loading || !profile) return <Loading />;
return (
<div className="listItem">
<Avatar src={profile.avatar} />
<div>
<h4>{profile.fullName}</h4>
<p>{profile.bio}</p>
</div>
</div>
);
}
render(<ProfileDetail />);
```
### Conditional
`null` will avoid binding and fetching data
```ts title="Resources"
import { Entity, resource } from '@data-client/rest';
export class Post extends Entity {
id = 0;
userId = 0;
title = '';
body = '';
static key = 'Post';
}
export const PostResource = resource({
path: '/posts/:id',
schema: Post,
});
export class User extends Entity {
id = 0;
name = '';
username = '';
email = '';
phone = '';
website = '';
get profileImage() {
return `https://i.pravatar.cc/64?img=${this.id + 4}`;
}
static key = 'User';
}
export const UserResource = resource({
urlPrefix: 'https://jsonplaceholder.typicode.com',
path: '/users/:id',
schema: User,
});
```
```tsx title="PostWithAuthor" {8-12}
import { useDLE } from '@data-client/react';
import { PostResource, UserResource } from './Resources';
export default function PostWithAuthor({ id }: { id: string }) {
const postDLE = useDLE(PostResource.get, { id });
const authorDLE = useDLE(
UserResource.get,
postDLE.data?.userId
? {
id: postDLE.data.userId,
}
: null,
);
if (postDLE.error) return <div>Error {`${postDLE.error.status}`}</div>;
if (postDLE.loading || !postDLE.data) return <Loading />;
if (authorDLE.error)
return <div>Error {`${authorDLE.error.status}`}</div>;
if (authorDLE.loading || !authorDLE.data) return <Loading />;
return <div>{authorDLE.data.username}</div>;
}
```
### Embedded data
When entities are stored in [nested structures](https://dataclient.io/rest/guides/relational-data#nesting), that structure will remain.
```typescript title="api/Post" {14-18}
import { Entity, RestEndpoint, Collection } from '@data-client/rest';
export class PaginatedPost extends Entity {
id = '';
title = '';
content = '';
static key = 'PaginatedPost';
}
export const getPosts = new RestEndpoint({
path: '/post',
searchParams: { page: '' },
schema: {
results: new Collection([PaginatedPost]),
nextPage: '',
lastPage: '',
},
});
```
```tsx title="ArticleList" {8}
import { useDLE } from '@data-client/react';
import { getPosts } from './api/Post';
export default function ArticleList({ page }: { page: string }) {
const { data, loading, error } = useDLE(getPosts, { page });
if (error) return <div>Error {`${error.status}`}</div>;
if (loading || !data) return <Loading />;
const { results: posts, nextPage, lastPage } = data;
return (
<div>
{posts.map(post => (
<div key={post.pk()}>{post.title}</div>
))}
</div>
);
}
```
### Github Reactions
`useDLE()` allows us to declaratively fetch reactions on any issue page the moment we navigate to it. This allows
us to not block the issues page from showing if the reactions are not completed loading.
It's usually better to wrap cases like this in new [Suspense Boundaries](./data-dependency.md#boundaries).
However, our component library `ant design` does not allow this.
Example app: [github-app](https://github.com/reactive/data-client/tree/master/examples/github-app) ([`src/resources/Reaction.tsx`](https://github.com/reactive/data-client/blob/master/examples/github-app/src/resources/Reaction.tsx), [`src/pages/IssueDetail/index.tsx`](https://github.com/reactive/data-client/blob/master/examples/github-app/src/pages/IssueDetail/index.tsx))
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |