Hooks
useWeb3Modal
Control the modal with the useWeb3Modal
hook
- Wagmi v1
- Ethers
- Ethers v5
import { useWeb3Modal } from '@web3modal/wagmi-react-native'
export default function Component() {
const { open, close } = useWeb3Modal()
open()
}
import { useWeb3Modal } from '@web3modal/ethers-react-native'
export default function Component() {
const { open, close } = useWeb3Modal()
open()
}
import { useWeb3Modal } from '@web3modal/ethers5-react-native'
export default function Component() {
const { open, close } = useWeb3Modal()
open()
}
You can also select the modal's view when calling the open
function
open({ view: 'Account' })
List of views you can select
Variable | Description |
---|---|
Connect | Principal view of the modal - default view when disconnected |
Account | User profile - default view when connected |
Networks | List of available networks - you can select and target a specific network before connecting |
WhatIsANetwork | "What is a network" onboarding view |
WhatIsAWallet | "What is a wallet" onboarding view |
useWeb3ModalState
Get the current value of the modal's state
- Wagmi v1
- Ethers
- Ethers v5
import { useWeb3ModalState } from '@web3modal/wagmi-react-native'
const { open, selectedNetworkId } = useWeb3ModalState()
import { useWeb3ModalState } from '@web3modal/ethers-react-native'
const { open, selectedNetworkId } = useWeb3ModalState()
import { useWeb3ModalState } from '@web3modal/ethers5-react-native'
const { open, selectedNetworkId } = useWeb3ModalState()
The modal state consists of two reactive values:
State | Description | Type |
---|---|---|
open | Open state will be true when the modal is open and false when closed. | boolean |
selectedNetworkId | The current chain id selected by the user | number |
useWeb3ModalEvents
Get the last tracked modal event
- Wagmi v1
- Ethers
- Ethers v5
import { useWeb3ModalEvents } from '@web3modal/wagmi-react-native'
const event = useWeb3ModalEvents()
import { useWeb3ModalEvents } from '@web3modal/ethers-react-native'
const event = useWeb3ModalEvents()
import { useWeb3ModalEvents } from '@web3modal/ethers5-react-native'
const event = useWeb3ModalEvents()
Ethereum Library
- Wagmi v1
- Ethers
- Ethers v5
You can use Wagmi hooks to sign messages, interact with smart contracts, and much more.
useAccount
Hook for accessing account data and connection status.
import { Text } from 'react-native'
import { useAccount } from 'wagmi'
function App() {
const { address, isConnecting, isDisconnected } = useAccount()
if (isConnecting) return <Text>Connecting…</Text>
if (isDisconnected) return <Text>Disconnected</Text>
return <Text>{address}</Text>
}
useSignMessage
Hook for signing messages with connected account.
import { View, Text, Pressable } from 'react-native'
import { useSignMessage } from 'wagmi'
function App() {
const { data, isError, isLoading, isSuccess, signMessage } = useSignMessage({
message: 'gm wagmi frens'
})
return (
<View>
<Pressable disabled={isLoading} onPress={() => signMessage()}>
<Text>Sign message</Text>
</Pressable>
{isSuccess && <Text>Signature: {data}</Text>}
{isError && <Text>Error signing message</Text>}
</View>
)
}
useContractRead
Hook for calling a read method on a Contract.
import { View, Text } from 'react-native'
import { useContractRead } from 'wagmi'
function App() {
const { data, isError, isLoading, isSuccess } = useContractRead({
address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
abi: wagmigotchiABI,
functionName: 'getHunger'
})
return (
<View>
{isLoading && <Text>Loading</Text>}
{isSuccess && <Text>Response: {data?.toString()}</Text>}
{isError && <Text>Error reading contract</Text>}
</View>
)
}
useWeb3ModalAccount
Hook that returns the client's information.
import { useWeb3ModalAccount } from '@web3modal/ethers-react-native'
function Components() {
const { address, chainId, isConnected } = useWeb3ModalAccount()
//...
}
useWeb3ModalProvider
Hook that returns the walletProvider
and the WalletProviderType
.
import { BrowserProvider } from 'ethers'
import { useWeb3ModalProvider } from '@web3modal/ethers-react-native'
function Components() {
const { walletProvider } = useWeb3ModalProvider()
async function onSignMessage() {
const ethersProvider = new BrowserProvider(walletProvider)
const signer = await ethersProvider.getSigner()
const message = 'hello web3modal + ethers'
const signature = await signer.signMessage(message)
console.log(signature.toString())
}
return <button onClick={() => onSignMessage()}>Sign Message</button>
}
useWeb3ModalError
import { useWeb3ModalError } from '@web3modal/ethers-react-native'
function Components() {
const { error } = useWeb3ModalError()
//...
}
useWeb3ModalAccount
Hook that returns the client's information.
import { useWeb3ModalAccount } from '@web3modal/ethers5-react-native'
function Components() {
const { address, chainId, isConnected } = useWeb3ModalAccount()
//...
}
useWeb3ModalProvider
Hook that returns the walletProvider
and the WalletProviderType
.
import { ethers } from 'ethers'
import { useWeb3ModalProvider } from '@web3modal/ethers5-react-native'
function Components() {
const { walletProvider } = useWeb3ModalProvider()
async function onSignMessage() {
const provider = new ethers.providers.Web3Provider(walletProvider)
const signer = provider.getSigner()
const signature = await signer?.signMessage('Hello Web3Modal Ethers')
console.log(signature)
}
return <button onClick={() => onSignMessage()}>Sign Message</button>
}
useWeb3ModalError
import { useWeb3ModalError } from '@web3modal/ethers5-react-native'
function Components() {
const { error } = useWeb3ModalError()
//...
}
Was this helpful?