-
Notifications
You must be signed in to change notification settings - Fork 4
React Patterns
IC Reactor supports a few distinct React integration styles. Choosing the right one keeps your app simpler and avoids mixing component-only hooks with imperative code.
Use this when components can pass functionName and args inline.
import { createActorHooks } from "@ic-reactor/react"
export const { useActorQuery, useActorMutation, useActorMethod } =
createActorHooks(backend)Good fit:
- straightforward React apps
- one hook suite per canister
- minimal setup
Use factories when the same operation must work both inside and outside React.
import { createQuery, createMutation } from "@ic-reactor/react"
export const getProfile = createQuery(backend, {
functionName: "get_profile",
})
export const updateProfile = createMutation(backend, {
functionName: "update_profile",
})Inside React:
const { data } = getProfile.useQuery()
const mutation = updateProfile.useMutation({
invalidateQueries: [getProfile.getQueryKey()],
})Outside React:
await getProfile.fetch()
await updateProfile.execute([{ name: "Alice" }])Good fit:
- route loaders and actions
- shared service modules
- targeted invalidation
- test helpers
Use useReactorQuery, useReactorMutation, and friends when you want to pass
the reactor instance at call time instead of binding it upfront.
This is usually useful in shared abstractions or when the reactor is selected dynamically.
Use these when a component wants one imperative interface that can handle query
and update methods through call(...).
Use Reactor when you want raw Candid types:
bigintPrincipal- raw blobs
Use DisplayReactor when you want UI-friendly values:
- numeric values as strings
- principals as text
- friendlier inputs for forms
- Components only: start with
createActorHooks(...) - Shared operations across React and non-React code: use factories
- Dynamic canister or runtime method work: use reactor hooks or candid packages