@pmcollab/coworkstream-react-query
v0.1.0
Published
TanStack Query (react-query) hooks for @pmcollab/coworkstream — fetching + optimistic mutations.
Readme
@pmcollab/coworkstream-react-query
TanStack Query (a.k.a. react-query) hooks for @pmcollab/coworkstream.
Install
npm install @pmcollab/coworkstream-react-query @tanstack/react-queryUse
import { WorkStream } from '@pmcollab/coworkstream'
import { createWorkStreamQueries } from '@pmcollab/coworkstream-react-query'
const { useItems, useItemAction } = createWorkStreamQueries({
queryKey: ['inbox'],
fetchItems: () => fetch('/api/inbox').then((r) => r.json()),
itemAction: ({ action, item }) =>
fetch(`/api/inbox/${item.id}/${action}`, { method: 'POST' }),
})
function Inbox() {
const { data: items = [], isLoading } = useItems()
const action = useItemAction()
return (
<WorkStream
items={items}
isLoading={isLoading}
onAction={(verb, item) => action.mutate({ action: verb, item })}
/>
)
}What it does
useItems— wrapsuseQuerywith yourfetchItems.useItemAction— wrapsuseMutationwith optimistic removal: the item disappears from the cache immediately, and rolls back ifitemActionrejects. Always invalidates on settle.
Pass optimistic: false to keep items in the list (useful for in-place edits).
License
Commercial. See LICENSE in the repository root.
