logo
Orfeas Voutsaridis
profile

Orfeas Voutsaridis

Software Engineer

linkedin
linkedin

Follow the link: Getting Started | Create React App

Create React App

Then we need to add the WebReactProvider in the index.tsx so we can use the library in the component. Then we need to create utils where the library is set.

WebReactProvider

In the chains.ts we can add the networks that we want to use for the Ethereum Blockchain.

Chains Configuration

We can add more than one network. If you use Ganache here in this file you can add your network name and chain id. In this project, we use only the networks that are public.

Next, we need to add custom hooks to handle web3 connect.

Custom Hooks

The file useConnectWeb3

useConnectWeb3

In the supportedChainIds we can add all the networks that we like to use “Mainnet” etc. We can check all the Chain Ids in this link: CoinTool

UseWebReact

UseWebReact

From the hook useWeb3React we can get all the important things so we can handle the login and other functionalities in Metamask. When the network is changing the useEffect trigger and we store the network id and with the chain.tsx we can map and extract the name so we can display it.

Network Handling

From the hook, we return the network value and we can use it in the project.

Network Value

Also, we can check if the user has installed the Metamask and display some messages. Now we can check if the user is not connected then we can use this function to connect in the project with the simple button.

Connect Button

Now we can use the hook from the library in the app.tsx and we can extract:

App Component

And use it like this in the app.tsx:

Final Implementation

And that is a simple way to use it in your project, check the GitHub code.

GitHub - erevos-13/connect-web3