Skip to main content

Hooks Overview

Scaffold Stark React Native provides a set of custom hooks that simplify interacting with Starknet smart contracts from your mobile application. These hooks are designed to work seamlessly with React Native and provide type-safe contract interactions.

Available Hooksโ€‹

Contract Interaction Hooksโ€‹

HookDescription
useScaffoldReadContractRead data from smart contract view functions
useScaffoldWriteContractExecute write transactions on smart contracts
useScaffoldMultiWriteContractExecute multiple transactions in a single batch
useScaffoldContractGet a typed contract instance (starknet.Contract with ABI)
useDeployedContractInfoGet deployed contract address and ABI
useScaffoldEventHistoryQuery historical contract events
useScaffoldWatchContractEventWatch a specific contract event in real-time

Account & Profile Hooksโ€‹

HookDescription
useScaffoldStrkBalanceGet STRK token balance for an address
useScaffoldStarkProfileFetch Starknet ID profile (name, avatar) for an address

Network Hooksโ€‹

HookDescription
useTargetNetworkGet current target network configuration
useSwitchNetworkSwitch between available networks
useNetworkColorGet theme-aware network display color

Wallet & Connection Hooksโ€‹

HookDescription
useAutoConnectAuto-reconnect wallet on app load (respects manual disconnect and TTL)

Low-Level Hooksโ€‹

HookDescription
useTransactorLow-level transaction execution with fee estimation and notifications
useNativeCurrencyPricePoll and cache native currency price
useAnimationConfigGet theme-aware animation configuration

WebSocket Hooksโ€‹

HookDescription
useWebSocketDataGeneric WebSocket data fetching
useScaffoldWebSocketEventsWatch contract events via WebSocket

Comparison with Web Hooksโ€‹

The React Native hooks are designed to mirror the Scaffold Stark 2 web hooks as closely as possible, with minor adaptations for the mobile environment:

FeatureWeb (SS2)React Native
API SignatureIdenticalIdentical
Return TypesSameSame
Wallet IntegrationBrowser walletsCavos Aegis + Burner Wallet
Toast Notificationsreact-hot-toasttoastify-react-native
Network SwitchingWallet promptIn-app handling
Secure StoragelocalStorageexpo-secure-store

Type Safetyโ€‹

All hooks are fully typed with TypeScript. Contract function names and arguments are inferred from your deployed contract ABIs:

// TypeScript will autocomplete function names and validate args
const { data } = useScaffoldReadContract({
contractName: "YourContract", // Autocompleted from deployed contracts
functionName: "getUserBalance", // Autocompleted from contract ABI
args: ["0x123..."], // Type-checked against function signature
});

Using starknet-react Hooksโ€‹

Scaffold Stark RN hooks are built on top of @starknet-react/core. You can also use starknet-react hooks directly for lower-level operations:

import { useAccount, useConnect, useDisconnect } from "@starknet-react/core";

// Get connected account
const { account, address, status } = useAccount();

// Handle wallet connection
const { connect, connectors } = useConnect();
const { disconnect } = useDisconnect();

Best Practicesโ€‹

1. Conditional Rendering Based on Loading Stateโ€‹

const { data, isLoading, error } = useScaffoldReadContract({
contractName: "YourContract",
functionName: "getValue",
});

if (isLoading) {
return <ActivityIndicator />;
}

if (error) {
return <Text>Error: {error.message}</Text>;
}

return <Text>Value: {data?.toString()}</Text>;

2. Handling Undefined Argsโ€‹

Hooks automatically disable when args contain undefined values:

const { address } = useAccount();

// This won't execute until address is defined
const { data } = useScaffoldReadContract({
contractName: "YourContract",
functionName: "getUserBalance",
args: [address], // Safe even if address is undefined
});

3. Refreshing Dataโ€‹

Use the refetch function for manual data refresh:

const { data, refetch } = useScaffoldReadContract({
contractName: "YourContract",
functionName: "getValue",
});

// Manual refresh
<Button title="Refresh" onPress={() => refetch()} />

4. Transaction Feedbackโ€‹

Provide user feedback during transactions:

const { sendAsync, isLoading, status } = useScaffoldWriteContract({
contractName: "YourContract",
functionName: "setValue",
args: [newValue],
});

// Show loading state
<Button
title={isLoading ? "Processing..." : "Submit"}
onPress={sendAsync}
disabled={isLoading}
/>

Quick Reference: Additional Hooksโ€‹

useScaffoldContractโ€‹

Get a typed contract instance for direct interaction:

const { data: contract, isLoading } = useScaffoldContract({
contractName: "YourContract",
});

// Use the starknet.Contract instance directly
const result = await contract?.call("myMethod", [arg1, arg2]);

useScaffoldStarkProfileโ€‹

Fetch Starknet ID profile data for an address:

const { data: profile, isLoading } = useScaffoldStarkProfile(address);
// profile: { name, profilePicture }

useSwitchNetworkโ€‹

Switch between configured networks:

const { switchNetwork, availableNetworks } = useSwitchNetwork();
await switchNetwork("mainnet");

Next Stepsโ€‹

Explore individual hook documentation for detailed usage examples: