Web3-React (V8)
This tutorial is a step-by-step guide on how to integrate a wallet such as Phantom into your dApp using the Web3-React (v8) library. Web3-React is an EVM library that exposes hooks for common web3 primitives such as wallets, addresses, and the like.
We will be going through step by step how to go from zero to a fully integrated web3-react-powered Phantom connect button.
Prerequisites
Node version >=16.12.0
A text editor/IDE (such as VSCode)
Some Knowledge of React
Yarn (v1)
Creating The App
We recommend using Vite to create new react applications.
To create a new React application using Vite, run the following command in your terminal:
This will ask you for a project name. Provide it a name here. For purposes of this tutorial I used "Web3-React-V8-Sandbox".
It will then ask you to select a framework. Select "React" here.
Next it will ask for a variant. Select "Typescript" here.
Now change directory into your project and run:
And make sure your app runs by running the command:
Configuring TypeScript
You will need to change the moduleResolution
keys to be switched from "bundler" to "node".
Each file should look as follows.
With that out of the way we can install all of our dependencies.
Installing Dependencies
To install the appropriate packages, run the following:
Now you can use the web3-react and web3-react-phantom packages in your project and integrate Phantom as a wallet in the app.
Initializing The Connector
Web3-react is entirely based around the idea of connectors, and how it handles them. In a nutshell, you configure all of the wallets you'll need up front, and then web3-react works as a huge global state machine, that allows you to interact with any wallet anywhere in your application.
To begin, create a file in the root of your directory called connectors.ts
it should look like the following
Here you Initialize all of the connectors that you want in your app, and then you can pass them all to the connectors
array, and export it for consumption in your app.
Initializing Web3-React
First we will need to import the packages into our project. At the top of the main.tsx
file add these imports:
Next, we will configure web3-react like so.
This will wrap our Vite app in this <Web3ReactProvider>
JSX component which will give us global access to the hooks that we need to interact with Phantom.
We also map through all of the arrays as the connector has 3 elements by default, and the <Web3ReactProvider>
element only expects 2, so we want to pull the 3rd one out before passing it to the wrapper.
We have everything we need done to add a connect wallet button to the app. But before we do, let's create a Card
component
Card Component
In the root of your app you can create a folder called components
and inside of that folder, create a file and name it Card.tsx
While this may look a bit complicated, we can break it down a bit.
What we will be passing the component are connector
, hooks
, and name
props.
We can run through what each of them is going to do.
connector
You can think of this as the wallet itself. It will be directly responsible for connecting, disconnecting, and sending requests to the blockchain.
hooks
These are an assortment of convenience hooks that will grab information about the wallet/connection that you are handling.
name
This is a string that we'll use to label the name of the connection to display in the UI.
The rest of the component is calling the hooks to get the relevant status connection, address, and chain id. Then we add some state management to display whether or not the wallet is connected, disconnected, or in the process of connecting.
Then we display all of the relevant information, and and show connect/disconnect buttons.
Adding The Card To The App
At the top of your App.tsx
file you can import the Card
component we just made and the useWeb3React
hook that we will use to access the values we stored in the wrapper up in main.tsx
.
Next, you will call the useWeb3React
hook at the top of your App
component
Then delete the useState hook, it's import, and the button that increments it as we do not need the counter anymore.
Then add the Card
component and pass in the connector and hooks into it. Where the counter button used to be.
Your entire App.tsx
file should look like so
Now when you run everything locally it should look like this!
And then once you click the connect button, it will pop open phantom's connect screen. Once you hit approve all of your information should be displayed like so
Conclusion
You've now added your very own Phantom connect button using Web3-React!
You can use this as the basis for your own dApp if you're looking to build out something, or use it for reference to add a Phantom connect button to your existing dApp.
Either way, we hope you found this useful.
Last updated