Reactive primitives for Ethereum apps
For documentation and guides, visit wagmi.sh.
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
For casual chit-chat with others using the framework:
Contributions to Wagmi are greatly appreciated! If you're interested in contributing to Wagmi, please read the Contributing Guide before submitting a pull request.
Here are some examples and use cases to help you understand how to use Wagmi:
import { useAccount, useBalance } from 'wagmi'
function AccountBalance() {
const { address } = useAccount()
const { data, isError, isLoading } = useBalance({
address,
})
if (isLoading) return <div>Fetching balance...</div>
if (isError) return <div>Error fetching balance</div>
return <div>Balance: {data?.formatted} {data?.symbol}</div>
}
import { useAccount, useSendTransaction } from 'wagmi'
import { parseEther } from 'ethers/lib/utils'
function SendTransaction() {
const { address } = useAccount()
const { sendTransaction } = useSendTransaction()
const handleSend = async () => {
try {
await sendTransaction({
to: '0xRecipientAddress',
value: parseEther('0.01'),
})
alert('Transaction sent!')
} catch (error) {
alert('Error sending transaction')
}
}
return (
<div>
<button onClick={handleSend}>Send 0.01 ETH</button>
</div>
)
}
import { useContractRead } from 'wagmi'
import { abi } from './MyContract.json'
function ReadContract() {
const { data, isError, isLoading } = useContractRead({
address: '0xContractAddress',
abi,
functionName: 'myFunction',
})
if (isLoading) return <div>Loading...</div>
if (isError) return <div>Error reading contract</div>
return <div>Data: {data}</div>
}
If you find Wagmi useful or use it for work, please consider sponsoring Wagmi. Thank you 🙏