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/useDebounce.md
View on GitHub<!-- Generated by `yarn build:skills` from docs/core/api/useDebounce.md (react). Edit the source doc, not this file. -->
# useDebounce()
Delays updating the parameters by [debouncing](https://css-tricks.com/debouncing-throttling-explained-examples/).
Useful to avoid spamming network requests when parameters might change quickly (like a typeahead field).
To fetch at once and abort the previous fetch, use [useCancelling()](https://dataclient.io/docs/api/useCancelling#cancel-vs-debounce).
> **Tip: React 18+**
>
> When loading new data, the [AsyncBoundary](./AsyncBoundary.md) will continue rendering the previous data until it is ready.
> `isPending` will be true while loading.
## Usage
```ts title="resources/Issue"
import { Entity, RestEndpoint } from '@data-client/rest';
export class Issue extends Entity {
id = 0;
number = 0;
title = '';
user = { login: '', avatar_url: '' };
static key = 'Issue';
}
export const searchIssues = new RestEndpoint({
urlPrefix: 'https://api.github.com',
path: '/search/issues',
searchParams: {} as { q: string },
schema: { items: [Issue] },
});
```
```tsx title="IssueList"
import React from 'react';
import { useSuspense } from '@data-client/react';
import { searchIssues } from './resources/Issue';
function IssueList({ search, stale }: Props) {
const { items } = useSuspense(searchIssues, {
q: `${search} repo:facebook/react is:issue`,
});
if (!items.length) return <p className="meta">No issues match</p>;
return (
<div className="list" aria-busy={stale}>
{items.map(issue => (
<div key={issue.pk()} className="listItem">
<Avatar src={issue.user.avatar_url} />
<div>
<h4>{issue.title}</h4>
<small>
#{issue.number} by {issue.user.login}
</small>
</div>
</div>
))}
</div>
);
}
// re-renders when its props change, not on every keystroke
export default React.memo(IssueList);
interface Props {
search: string;
/** still the last search's: the query changed since (React's isPending
* starts only once the new search does) */
stale: boolean;
}
```
```tsx title="SearchIssues" {7}
import React from 'react';
import { AsyncBoundary, useDebounce } from '@data-client/react';
import IssueList from './IssueList';
export default function SearchIssues() {
const [query, setQuery] = React.useState('');
const [debouncedQuery, isPending] = useDebounce(query, 200);
const input = React.useRef<HTMLInputElement>(null);
return (
<>
<header className="appHeader">
<h1 className="appTitle">Issues</h1>
</header>
<label className="search" aria-busy={isPending}>
<input
ref={input}
type="search"
aria-label="Search issues"
placeholder="Search issues"
value={query}
onChange={e => setQuery(e.currentTarget.value)}
/>
<button
type="button"
className="clear"
aria-label="Clear search"
onClick={() => {
setQuery('');
input.current?.focus();
}}
/>
</label>
<AsyncBoundary fallback={<Loading />} errorComponent={ErrorMessage}>
<IssueList search={debouncedQuery} stale={query !== debouncedQuery} />
</AsyncBoundary>
</>
);
}
render(<SearchIssues />);
```
## Types
```typescript
function useDebounce<T>(
value: T,
delay: number,
updatable?: boolean,
): [T, boolean];
```
Returns `[debouncedValue, isPending]`. When `updatable` is `false`, the debounced value does not update.
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 |