Docs
/Hooks
/useContractWrite
useContractWrite
Hook for calling a ethers.js Contract write method.
import { useContractWrite } from 'wagmi'
Usage
The following examples use the WAGMIGOTCHI Contract.
import { useContractWrite } from 'wagmi'
function App() {
const { data, isError, isLoading, write } = useContractWrite(
{
addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
contractInterface: wagmigotchiABI,
},
'feed',
)
return <button onClick={write}>Feed</button>
}
Return Value
{
data?: TransactionResponse
error?: Error
isError: boolean
isIdle: boolean
isLoading: boolean
isSuccess: boolean
write: (config?: WriteContractConfig) => void
writeAsync: (config?: WriteContractConfig) => Promise<TransactionResponse>
reset: () => void
status: 'idle' | 'error' | 'loading' | 'success'
}
Arguments
contractConfig
See useContract for more info.
functionName
Name of function to call.
import { useContractWrite } from 'wagmi'
function App() {
const contractWrite = useContractWrite(
{
addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
contractInterface: wagmigotchiABI,
},
'feed',
)
return <button onClick={write}>Feed</button>
}
Configuration
args (optional)
Arguments to pass to function call. Accepts any | any[]
.
import { useContractWrite } from 'wagmi'
function App() {
const contractWrite = useContractWrite(
{
addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
contractInterface: wagmigotchiABI,
},
'feed',
{
args: [],
},
)
}
overrides (optional)
Overrides to pass to function call.
import { useContractWrite } from 'wagmi'
function App() {
const contractWrite = useContractWrite(
{
addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
contractInterface: wagmigotchiABI,
},
'feed',
{
overrides: { from: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' },
},
)
}
onError (optional)
Function to invoke when an error is thrown while attempting to write.
import { useContractWrite } from 'wagmi'
function App() {
const contractWrite = useContractWrite(
{
addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
contractInterface: wagmigotchiABI,
},
'feed',
{
onError(error) {
console.log('Error', error)
},
},
)
}
onMutate (optional)
Function fires before write function and is passed same variables write function would receive. Value returned from this function will be passed to both onError
and onSettled
functions in event of a write failure.
import { useContractWrite } from 'wagmi'
function App() {
const contractWrite = useContractWrite(
{
addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
contractInterface: wagmigotchiABI,
},
'feed',
{
onMutate({ args, overrides }) {
console.log('Mutate', { args, overrides })
},
},
)
}
onSettled (optional)
Function to invoke when write is settled (either successfully written, or an error has thrown).
import { useContractWrite } from 'wagmi'
function App() {
const contractWrite = useContractWrite(
{
addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
contractInterface: wagmigotchiABI,
},
'feed',
{
onSettled(data, error) {
console.log('Settled', { data, error })
},
},
)
}
onSuccess (optional)
Function to invoke when write is successful
import { useContractWrite } from 'wagmi'
function App() {
const contractWrite = useContractWrite(
{
addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
contractInterface: wagmigotchiABI,
},
'feed',
{
onSuccess(data) {
console.log('Success', data)
},
},
)
}