useReactorQuery
useReactorQuery is a React hook for fetching data from Internet Computer canisters using a Reactor instance. It is the raw version of useActorQuery that requires explicit reactor passing.
Import
Section titled “Import”import { useReactorQuery } from "@ic-reactor/react"import { useReactorQuery } from "@ic-reactor/react"import { backend } from "./reactor"
function UserProfile({ userId }: { userId: string }) { const { data, isPending, error } = useReactorQuery({ reactor: backend, // 👈 Required functionName: "get_user", args: [userId], })
if (isPending) return <div>Loading...</div> if (error) return <div>Error: {error.message}</div>
return <div>{data?.name}</div>}Options
Section titled “Options”Required Options
Section titled “Required Options”| Option | Type | Description |
|---|---|---|
reactor |
Reactor<A, T> |
The Reactor instance to use |
functionName |
string |
The canister method to call |
Optional Options
Section titled “Optional Options”Same as useActorQuery.
Return Value
Section titled “Return Value”Returns a TanStack Query result object, same as useActorQuery.
Examples
Section titled “Examples”Basic Query
Section titled “Basic Query”import { useReactorQuery } from "@ic-reactor/react"import { backend } from "./reactor"
function Greeting() { const { data } = useReactorQuery({ reactor: backend, functionName: "greet", args: ["World"], })
return <span>{data}</span>}Waiting for Args
Section titled “Waiting for Args”args takes TanStack Query’s skipToken while the arguments are not known;
see useActorQuery:
import { skipToken, useReactorQuery } from "@ic-reactor/react"import { backend } from "./reactor"
function UserName({ userId }: { userId: string | undefined }) { const { data } = useReactorQuery({ reactor: backend, functionName: "get_user", args: userId ? [userId] : skipToken, })
return <span>{data?.name}</span>}Type-Safe Queries
Section titled “Type-Safe Queries”TypeScript infers types from the passed reactor instance:
import { useReactorQuery } from "@ic-reactor/react"import { backend } from "./reactor"
function User() { const { data } = useReactorQuery({ reactor: backend, functionName: "get_user", // ✅ Autocompletes valid methods args: ["user-123"], // ✅ Type-checked })
return <span>{data?.name}</span>}See Also
Section titled “See Also”- useActorQuery — Bound version
- useReactorSuspenseQuery — Suspense version
- Reactor Hooks Overview