e30e75b5d4
Changesets / Create Version PR (push) Has been cancelled
Deploy Shadcn Registry / Deploy Production (push) Has been cancelled
Template Metrics / LOC + Bundle Size (push) Has been cancelled
Code Quality / Oxlint + Oxfmt (push) Has been cancelled
Code Quality / Template Sync (push) Has been cancelled
Code Quality / Build Changed Packages (push) Has been cancelled
Code Quality / Test Changed Packages (push) Has been cancelled
Deploy Expo Example / Deploy Production (push) Has been cancelled
Deploy Ink Example / Deploy Production (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.12) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Has been cancelled
61 lines
1.5 KiB
Plaintext
61 lines
1.5 KiB
Plaintext
---
|
|
title: Quickstart
|
|
description: Install tap and build your first resource.
|
|
---
|
|
|
|
## Install
|
|
|
|
```sh
|
|
npm install @assistant-ui/tap
|
|
```
|
|
|
|
## Define a resource
|
|
|
|
A resource is a React component without the UI. Write a `use`-prefixed hook with
|
|
the hooks you already know, imported from `"react"`, then wrap it with `resource`.
|
|
|
|
```ts
|
|
import { resource } from "@assistant-ui/tap";
|
|
import { useState } from "react";
|
|
|
|
const useCounter = () => {
|
|
const [count, setCount] = useState(0);
|
|
return { count, increment: () => setCount((c) => c + 1) };
|
|
};
|
|
|
|
const Counter = resource(useCounter);
|
|
```
|
|
|
|
Instead of JSX, a resource returns a plain value: the state and methods you want
|
|
callers to use.
|
|
|
|
## Use it in React
|
|
|
|
`useResource` hosts a resource inside a React component. The component re-renders
|
|
whenever the resource's state changes, and unmounts the resource when it unmounts.
|
|
|
|
```tsx
|
|
import { useResource } from "@assistant-ui/tap";
|
|
|
|
function CounterButton() {
|
|
const { count, increment } = useResource(Counter());
|
|
|
|
return <button onClick={increment}>Count: {count}</button>;
|
|
}
|
|
```
|
|
|
|
`resource()` turns a hook into a Resource; `useResource(Counter())` turns it back
|
|
into a hook call. Calling the factory (`Counter()`) creates an inert element;
|
|
`useResource` brings it to life.
|
|
|
|
## Next steps
|
|
|
|
<Cards>
|
|
<Card title="Hooks" href="/tap/docs/tap/hooks">
|
|
React's hooks inside resources, plus tap's additions.
|
|
</Card>
|
|
<Card title="Composition" href="/tap/docs/tap/composition">
|
|
Nest resources with useResource and useResources.
|
|
</Card>
|
|
</Cards>
|