First-class React hooks for NEAR blockchain interactions
@near-kit/react provides thin, focused React hooks that wrap the core near-kit library. These hooks handle the React lifecycle for you while giving you full control over caching and data fetching strategies.
Design Philosophy: These hooks are intentionally thin wrappers. They manage loading states and errors, but don’t include built-in caching. For production apps, we recommend pairing them with React Query or SWR.
Here’s a complete example showing provider setup, reading data, and sending transactions:
Copy
import { Near } from "near-kit"import { NearProvider, useNear, useBalance, useSend } from "@near-kit/react"// 1. Create the Near clientconst near = new Near({ network: "testnet", privateKey: "ed25519:...", defaultSignerId: "alice.testnet",})// 2. Wrap your app with NearProviderfunction App() { return ( <NearProvider near={near}> <Wallet /> </NearProvider> )}// 3. Use hooks in any componentfunction Wallet() { const { data: balance, isLoading } = useBalance({ accountId: "alice.testnet" }) const { mutate: send, isPending } = useSend() const handleSend = async () => { await send("bob.testnet", "1 NEAR") } if (isLoading) return <p>Loading...</p> return ( <div> <p>Balance: {balance}</p> <button onClick={handleSend} disabled={isPending}> {isPending ? "Sending..." : "Send 1 NEAR"} </button> </div> )}
For production apps with mutations, we recommend using React Query with useNear() for proper cache invalidation. The thin hooks above are great for prototyping but don’t automatically refetch data after mutations.