useMetamask
Hook that prompts users to connect their MetaMask wallet to your app.
import { useMetamask } from "@thirdweb-dev/react";
The metamaskWallet also needs to be added in ThirdwebProvider's supportedWallets if you want the wallet to auto-connect on next page load.
Usage
Call the function returned by the useMetamask
hook to prompt the user to connect their MetaMask wallet to your dApp.
You can then use the useAddress
hook to get the user's address.
import { useMetamask } from "@thirdweb-dev/react";
function App() {
const connectWithMetamask = useMetamask();
return (
<button onClick={() => connectWithMetamask()}>Connect Metamask</button>
);
}
Configuration
chainId
To connect to a specific chain when connecting the wallet,
pass the chainId
in a configuration object as shown below.
This will prompt the user to switch to the given network after they connect.
import { useMetamask } from "@thirdweb-dev/react";
import { Dogechain } from "@thirdweb-dev/chains";
function App() {
const connectWithMetamask = useMetamask();
return (
<button
onClick={() =>
connectWithMetamask({
chainId: Dogechain.chainId,
})
}
>
Connect Wallet
</button>
);
}
If the chain is not configured in the user’s wallet,
you must add this chain in ThirdwebProvider
’s supportedChains prop:
import { Dogechain } from "@thirdweb-dev/chains";
import { ThirdwebProvider } from "@thirdweb-dev/react";
export function YourApp() {
return (
<ThirdwebProvider supportedChains={[Dogechain]} clientId="your-client-id">
<App />
</ThirdwebProvider>
);
}