create()
Combine a model and action factories into one domain hook.
import { create } from '@comwit/state'
import { post } from './model'
import { crudActions } from './actions/crud'
import { loadActions } from './actions/load'
import type { PostState, PostActions } from './types'
export const usePost = create<PostState, PostActions>(post, {
actions: [crudActions, loadActions],
})
For a read-only domain, pass { actions: [] }.
Select the state you render
const { title, actions } = usePost((s) => ({
title: s.detail.data?.title,
actions: s.actions,
}))
Selectors compare results with deep equality. This example updates when the title changes; selecting
s.detail instead would also observe its query status and other data. Calling usePost() without
a selector subscribes to the whole domain.
Server data with .suspend()
Where the server HTML matters (SEO: public lists and details), select .suspend(arg) in the
component that renders the data:
'use client'
// Inside <Suspense>: the server render runs queryFn and streams the result
// when ComwitProvider has useServerInsertedHTML.
function PostDetail({ slug }: { slug: string }) {
const post = usePost((s) => s.detail.suspend(slug))
return post.data ? <article>{post.data.title}</article> : null
}
No Server Component fetch or adapter is needed. queryFn runs on both server and browser.
Alternative: in a framework whose provider has no useServerInsertedHTML, a server response cannot
reach the client cache, so client navigation requests it again. Pass the server result to the hook's
hydrate(entries) method there. Next.js does not need it:
'use client'
// A Server Component awaited initialPost and passed it to this route adapter.
function PostRoute({ slug, initialPost }: Props) {
usePost.hydrate({ detail: { arg: slug, data: initialPost } })
return <PostView />
}
hydrate(entries) infers query field names, arguments, and data from the model. Call it
unconditionally before the normal domain read; it returns void and accepts null or undefined.
See the Next.js guide.
Load a query
For a field typed Query<Post[], { page: number }>, pass its argument in the selector:
const posts = usePost((s) => s.posts.load({ page }))
if (posts.isLoading) return <Skeleton />
if (posts.isError && !posts.isSuccess) return <p>{posts.error}</p>
return posts.data.map((post) => <Card key={post.id} post={post} />)
.load(arg) reports loading during render and starts the request after commit; screens that do not
need SEO (behind login, client-driven reads) may read this way. Selecting s.posts without
.load(...) only reads state. Use actions for imperative loading and coordinated workflows.
See query() for cache keys, freshness, and .suspend(arg).
Call an action
const actions = usePost((s) => s.actions)
return <button onClick={() => actions.like(postId)}>Like</button>
Actions are bound to the current provider. Keep writes inside actions and call them from events or imperative workflows. Do not initialize the store by calling an action during render.
Lower-level hooks
create() combines the two hooks below and attaches the typed hydration method. You can use them
separately when composing your own integration:
import { useModel, useAction } from '@comwit/state'
const title = useModel(post, (s) => s.detail.data?.title)
const actions = useAction<PostActions>([crudActions, loadActions])
Both require ComwitProvider. useModel() selectors expose the same query .load() interface.