tap: React hooks, headless
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);tap is React's hooks, headless. You write a resource the same way you write a
React component, with the same hooks (useState, useEffect, useMemo, ...)
imported from "react" and the same rules. The only difference is that a resource
has no UI: instead of returning JSX, it returns a plain value (state and methods),
and it runs anywhere, inside a React component, inside another resource, or
standalone with no React at all.
If you know React hooks, you already know tap. There are only three new things:
- You write a
use-prefixed hook and wrap it withresource(useName); the unit returns a value instead of JSX. - You instantiate it by calling the factory (
Name(props)) to get an element, then host it withuseResource. - A resource tree can run outside React entirely.