Web3 Wallet Connect


A simple step-by-step process for connecting a web3 wallet using RainbowKit and Wagmi';
'use client';
import '@rainbow-me/rainbowkit/styles.css';
import './globals.css';
import { ReactNode } from 'react';
import { RainbowKitProvider } from '@rainbow-me/rainbowkit';
import { getDefaultConfig } from '@rainbow-me/rainbowkit';
import { WagmiProvider } from 'wagmi';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { mainnet, polygon, arbitrum } from 'wagmi/chains';
const config = getDefaultConfig({
appName: 'Web3 Wallet App',
projectId: '64afa1699b960877418b3d5f02e104f2',
chains: [mainnet, polygon, arbitrum],
ssr: true,
});
const queryClient = new QueryClient();
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>
<WagmiProvider config={config}>
<QueryClientProvider client={queryClient}>
<RainbowKitProvider>
{children}
</RainbowKitProvider>
</QueryClientProvider>
</WagmiProvider>
</body>
</html>
);
}
To get started, we begin by installing the RainbowKit and Wagmi dependencies. These are needed to use the hooks required for setting up the connect wallet process. Run the following command:
npm install wagmi viem @rainbow-me/rainbowkit
Running this in your terminal will automatically start the installation process.
Think of Wagmi as the engine that powers the Ethereum logic on your frontend, and RainbowKit as the beautiful dashboard that wraps around Wagmi. after the installation we then start by importing the rainbowkit css styles together with the global css, the
RainbowKit styles contains default styles for the buttons, modals and the entire wallet, and also the ReactNode typscript type for react children elements,
RainbowKitProvider: this is majorly a context provider that wraps your whole app with wallet functionalities
getDefaultConfig: this function creates a pre-configured setup for common wallets type
wagmiProvider: this is a context provider for block chain interaction and statemanagement
QueryClient, QueryClientProvider: Data fetching and caching system (required by Wagmi v2)
mainnet, polygon, arbitrum: Pre-defined blockchain network configurations
so now the function const config creates configuration containg the appName, projectId, chains,and SSR
then declaring a cont query this automatically creates an instance of Tanstack Query for managing data fetching, caching, and synchronization. QueryClient () is a constructor that creates a new query management system.
then the return section returns the JSX structure with nested providers
Provider Hierarchy (Outside to Inside):
<html lang=”en”> Root Html element with the English language attribute
<body> Html body container
<wagmiprovider config={config}> Provides the blockchain functionalities using the config object.
<QueryClientProvider client={queryclient} Provides data fetching capabilities using the queryClient instance.
<RainbowKitProvider> Provide wallet connection Ui functionality.
{children} Renders Child componenets of your actual page.
Key JavaScript Function/Concepts Used
1.getDefaultConfig()
Type: Function call
Purpose: Factory function that returns a configuration object
Returns: Object with wallet connectors, chains, and settings
2. new QueryClient()
Type: Constructor function call
Purpose: Creates new instance of QueryClient class
Returns: QueryClient object for managing queries and cache
3.Destructuring Assignment
{ children }: { children: ReactNode }
Purpose: Extracts children
property from the props object parameter
4.JSX(JavaScript XML)
Purpose: Syntax extension that allows writing HTML-like code in JavaScript
Compiled to: React.createElement() function calls
5.Context Providers Patterns
Purpose: React pattern for sharing state/functionality across component tree
How it works: Each Provider makes its functionality available to all child components.
Subscribe to my newsletter
Read articles from aliyu abdulrazak directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
