TypeScript Demo
Pure TypeScript example showing IC Reactor without React, for Node.js or other non-React environments.
Features Demonstrated
Section titled “Features Demonstrated”Reactorusage without React hooks- Direct
callMethodfor queries and mutations getQueryOptions()for manual query execution- Works in Node.js, Deno, or any JavaScript runtime
- Command-line interface example
Open in StackBlitzEdit and run in your browser
View on GitHubBrowse the source code
Live Preview
Section titled “Live Preview”Key Code
Section titled “Key Code”Basic Setup
Section titled “Basic Setup”import { ClientManager, Reactor } from "@ic-reactor/core"import { QueryClient } from "@tanstack/query-core"import { ledgerIdlFactory } from "./declarations/ledger"import type { Ledger } from "./declarations/ledger.type"
const queryClient = new QueryClient()
const clientManager = new ClientManager({ agentOptions: { host: "https://ic0.app" }, queryClient,})
const ledgerReactor = new Reactor<Ledger>({ clientManager, canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai", idlFactory: ledgerIdlFactory, name: "Ledger",})Direct Method Calls
Section titled “Direct Method Calls”// Query method — fetchQuery goes through the cacheconst name = await ledgerReactor.fetchQuery({ functionName: "icrc1_name" })console.log("Token name:", name)
// Query with args — callMethod bypasses the cacheconst balance = await ledgerReactor.callMethod({ functionName: "icrc1_balance_of", args: [{ owner: principal, subaccount: [] }],})console.log("Balance:", balance)
// Update method (mutation)const blockIndex = await ledgerReactor.callMethod({ functionName: "icrc1_transfer", args: [ { to: { owner: recipientPrincipal, subaccount: [] }, amount: 100_000_000n, // 1 ICP in e8s fee: [], memo: [], created_at_time: [], from_subaccount: [], }, ],})console.log("Transfer block index:", blockIndex)
// Drop every cached query for this canister after a writeledgerReactor.invalidateQueries()Using QueryClient Directly
Section titled “Using QueryClient Directly”// Get query options for manual executionconst queryOptions = ledgerReactor.getQueryOptions({ functionName: "icrc1_name",})
// Execute with QueryClientconst result = await queryClient.fetchQuery(queryOptions)
// Or prefetch for laterawait queryClient.prefetchQuery(queryOptions)Installation for Non-React
Section titled “Installation for Non-React”pnpm add @ic-reactor/core @icp-sdk/core @tanstack/query-core