🎉 ⏱ Ready to build? Get early access now!⏱ 🎉
Connect Wallet button in React
Add a connect wallet button in React
Quick disclaimer
If you don't have a a wallet, click here. If you don't know how a connection works with the blockchain, click here. If you don't know how to instantiating our sdk works....click here. If all of this doesn't make sense...please go to our starting page 😬
Install packages
//Run this to install with npm npm install @3rdweb/react @3rdweb/hooks
Create the button
Inside your components
folder, create a file Connect.js
with the following code 👇
By wrapping it in the <nav>
tag, the button will appear in the nav bar.
import { ConnectWallet } from "@3rdweb/react"; const Connect = () => { return <nav> <ConnectWallet /> </nav>; };
Before we can use this button, we need to add the provider to our app.👇
Choose the chain and wallet (provider and signer)
Now we need to setup our Provider
. For this we will use the custom component of thirdweb, the ThirdwebProvider
. Go ahead and import it.
import { ThirdwebProvider } from "@3rdweb/react";
Next we need to select the chain and type of wallet we want to connect to.
With supportedChainIds
we select the blockchain we want to connect with. We have to pass an Id
. Every blockchain has an Id
. We are using an Id
instead of a RPC url
here. (again click here if you don't know how a connection with blockchain works).
const App = () => { // Polygon's chain Id is 137 const supportedChainIds = [137];
With connectors
we are selecting which wallets we want to support. If you want to learn more about our connect wallet component, click here.
MetaMask connector
setup 👇
//injected - MetaMask const connectors = { injected: {}, };
Coinbase connector
setup 👇
walletlink: { appName: "", url: "https://rinkeby-light.eth.linkpool.io/", darkMode: false, }, };
We include the ThirdwebProvider
in the return statement to wrap our app in the ThirdwebProvider
, so that the provider information is accessible from anywhere.
ThirdwebProvider
tags should be included at the highest level, usually in react that is app.js except in next. In next it's in _app.js
So make sure you put your Component
within the brackets of the ThirdwebProvider
.
return ( <ThirdwebProvider connectors={connectors} supportedChainIds={supportedChainIds} > <Connect /> /* our connect wallet button */ </ThirdwebProvider> ); };
Here's what the whole code looks like with Metamask
as an example in your App.js
file.
import { ThirdwebProvider } from "@3rdweb/react"; const App = () => { // Polygon's chain Id is 137 const supportedChainIds = [137]; //injected - MetaMask const connectors = { injected: {}, }; return ( <ThirdwebProvider connectors={connectors} supportedChainIds={supportedChainIds} > <Connect /> /* our connect wallet button */ </ThirdwebProvider> ); };