Hooks Reference
ethers-query provides a set of React hooks for interacting with Ethereum. Here's a comprehensive list of available hooks:
Account Management
useAccount
const {
address,
isConnected,
chainId,
connect,
disconnect
} = useAccount()
Manages wallet connection and account state.
useWallet
const {
connect,
disconnect,
isConnecting,
isDisconnecting,
error
} = useWallet()
Provides wallet connection management functions and connection states.
useBalance
const {
data: balance,
isLoading,
error
} = useBalance({ address })
Fetches the native token balance for an address.
Contract Interaction
useSmartContract
const contract = useSmartContract({
address: string,
abi: ContractInterface,
functionName?: string
})
Creates a contract instance for reading and writing.
useContractRead
const {
data,
isLoading,
error
} = useContractRead({
address,
abi,
functionName,
args
})
Reads data from a contract.
Signing
useSignMessage
const {
signMessage,
isLoading,
data: signature
} = useSignMessage()
Signs messages using the connected wallet.
useSignTypedData
const {
signTypedData,
isLoading,
data: signature
} = useSignTypedData()
Signs EIP-712 typed data.
Network
useProvider
const provider = useProvider()
Access the current ethers provider.
useSigner
const signer = useSigner()
Access the current ethers signer.
Common Properties
Most hooks return objects with these common properties:
isLoading
: Boolean indicating if an operation is in progresserror
: Any error that occurred during the operationdata
: The result data (type varies by hook)
Error Handling
All hooks use a consistent error pattern:
try {
await someHook.someAction()
} catch (error) {
if (error.code === 'ACTION_REJECTED') {
// User rejected the action
} else {
// Other errors
}
}
Best Practices
- Always check
isLoading
anderror
states - Use TypeScript for better type safety
- Handle errors gracefully
- Implement proper loading states
- Cache results when appropriate