Create your own
Lesson illustration

Integrating Solana Wallet Adapter in React

Hello! Welcome to the next phase of your Solana development journey.

So far, we've focused on the "backend" of Solana—the on-chain programs written in Rust. Now, we're making a significant and exciting shift to the "frontend": building the client-side applications that users interact with. Your extensive experience as a front-end developer will be a great asset here as we dive into connecting a web application to the Solana blockchain.

This lesson marks the beginning of our module on client-side development. Our goal is to integrate the @solana/wallet-adapter libraries into a React application. This is the foundational step for any decentralized application (dApp), as it enables users to connect their own wallets, approve transactions, and interact with your on-chain programs.

The Bridge Between Your App and the User's Wallet

In web2, you handle user authentication with email/password or OAuth. In web3, the user's wallet is their identity and their key to interacting with the blockchain. However, there are many different wallet providers (Phantom, Solflare, Ledger, etc.). Building bespoke integrations for each one would be a nightmare.

This is where the @solana/wallet-adapter library suite comes in. It provides a modular, standardized way to support a wide range of wallets with a single, clean API. It abstracts away the implementation details of each wallet, letting you focus on your application's logic.

From your experience with React, you can think of this library as providing a set of React Context Providers. It makes the wallet's connection status, the user's public key, and functions for signing transactions available to any component in your application, much like how Redux or React's own Context API provides global state.

The Core Components of Wallet-Adapter

The library is split into a few key packages:

  • @solana/wallet-adapter-react: Contains the core React hooks and components (ConnectionProvider, WalletProvider, useWallet).
  • @solana/wallet-adapter-react-ui: Provides pre-built UI components like buttons and modals for a quick and easy setup (WalletMultiButton, WalletModalProvider).
  • @solana/wallet-adapter-wallets: Includes the specific adapter logic for different wallets (e.g., PhantomWalletAdapter, SolflareWalletAdapter).
  • @solana/wallet-adapter-base: Defines the common interfaces and types that all adapters adhere to.

Let's see how these pieces fit together.

Step 1: Project Setup and Dependencies

First, you need a React or Next.js project. Then, you'll install the necessary packages. The Solana Cookbook provides a clear, concise list of the dependencies.

How to Connect a Wallet with React

This section of the Solana Cookbook lists the essential npm packages you'll need to install to get started with the wallet-adapter.

In the webpage, find the section titled 'Dependencies'. Review the npm install command to see the list of required packages. You don't need to run the command now, just familiarize yourself with the package names.

Step 2: The Provider Stack Architecture

The key to integrating the wallet adapter is wrapping your application in a specific hierarchy of provider components. Each provider has a distinct role:

  1. ConnectionProvider: This is the outermost wrapper. Its job is to establish and maintain a connection to a Solana RPC endpoint. You'll pass it the URL for the cluster you want to connect to (e.g., Devnet, Mainnet Beta). All child components will then have access to this connection.

  2. WalletProvider: This component sits inside ConnectionProvider. It manages the list of wallets you want your dApp to support. You provide it with an array of wallet adapter instances (e.g., new PhantomWalletAdapter()). It handles the logic of connecting, disconnecting, and keeping track of the selected wallet.

  3. WalletModalProvider: This is the innermost provider. It provides the beautiful, pre-styled modal that pops up when a user wants to select a wallet. This component works in tandem with the UI buttons from the @solana/wallet-adapter-react-ui package.

This nested structure ensures that the wallet modal has access to the list of wallets, and the wallet provider has access to the network connection.

The Solana Cookbook offers a very clean pattern for this: creating a single SolanaProvider component that encapsulates this entire stack. This is an excellent practice for keeping your main application file (_app.tsx or layout.tsx) clean.

How to Connect a Wallet with React

Let's look at a code example of this provider stack. This section of the Solana Cookbook demonstrates how to create a reusable component that wraps all the necessary wallet-adapter providers.

Find the section 'Create Solana Provider' and study the SolanaProvider.tsx code snippet. Note how ConnectionProvider, WalletProvider, and WalletModalProvider are nested. Then, look at the next section, 'Wrap the Application in the Solana Provider', to see how this custom provider is used to wrap the entire application layout.

Test your understanding!

Why is ConnectionProvider the outermost provider in the stack, while WalletModalProvider is the innermost? What would happen if you reversed their order?

Show answer

ConnectionProvider must be on the outside because it provides the fundamental connection to the Solana network that everything else depends on. The WalletProvider needs this connection to interact with the blockchain (e.g., to fetch balances). The WalletModalProvider needs the WalletProvider to know which wallets to display in the modal.

If you reversed the order, the components would not have access to the context they require from their parent providers, leading to runtime errors. For example, WalletProvider would fail because it couldn't find a network connection provided by ConnectionProvider.

Step 3: Adding the UI Button

Once the providers are in place, adding a connect button is remarkably simple. The @solana/wallet-adapter-react-ui package includes the WalletMultiButton component.

When you place this button anywhere inside your provider stack, it automatically handles everything:

  • It displays "Select Wallet" when the user is disconnected.
  • Clicking it opens the wallet selection modal (thanks to WalletModalProvider).
  • After connecting, it displays the user's truncated wallet address.
  • Clicking it again provides options to copy the address or disconnect.

Walkthrough: Putting It All Together

Reading about it is one thing, but seeing it in action is best. The following video from Helius provides a fantastic, step-by-step walkthrough of integrating the wallet adapter into a Next.js application. It covers everything we've discussed and shows how to display the wallet's connection status and balance in the UI.

Please watch the following segments carefully.

Use The Solana Wallet Adapter (Next.js, TypeScript, Tailwind CSS) • Solana Frontend Developer Course

This video will guide you through the practical implementation. The first segment covers the core logic: setting up the provider stack in the React component tree.

Watch the segment from 20:16 to 24:14. Pay close attention to how the instructor imports and nests ConnectionProvider, WalletProvider, and WalletModalProvider. This directly corresponds to the provider stack architecture we just discussed.

Now that the providers are set up, let's add the UI element that allows the user to interact with them.

Use The Solana Wallet Adapter (Next.js, TypeScript, Tailwind CSS) • Solana Frontend Developer Course

This next clip shows how to add the WalletMultiButton component to your UI. Notice how little code is needed to get a fully functional wallet connection flow.

Watch the segment from 26:42 to 28:32. The instructor demonstrates how to import and use the WalletMultiButton and even shows how to apply custom styling, which leverages your front-end skills.

Finally, to make the integration useful, your application needs to react to the wallet's state. The video goes on to demonstrate how to use the useWallet hook (which we'll cover in detail later) to get the user's public key and display whether the wallet is connected.

Use The Solana Wallet Adapter (Next.js, TypeScript, Tailwind CSS) • Solana Frontend Developer Course

This last segment demonstrates how to make the UI dynamic based on the wallet's connection state. It's a great preview of how you'll build interactive features.

Watch the segment from 29:48 to 35:13. Focus on the use of a ternary operator (publickey ? 'Yes' : 'No') to conditionally render the connection status. This shows how the state provided by the wallet adapter can be used to drive your application's UI.

Conclusion

In this lesson, you've taken the first crucial step into Solana dApp development. We've bridged the gap between a standard React application and the Solana ecosystem.

Key Takeaways:

  • The @solana/wallet-adapter libraries provide a standardized way to manage wallet connections in a front-end application.
  • The core architecture relies on a stack of React Context Providers: ConnectionProvider -> WalletProvider -> WalletModalProvider.
  • This provider stack must wrap your root application component to make wallet context available everywhere.
  • The WalletMultiButton is a convenient, pre-built UI component that handles the entire connection flow, from selection to disconnection.

With this foundation in place, your dApp is now "wallet-aware." It can identify the user via their public key.

Preview of the Next Lesson:

Now that we can connect a wallet, what can we do with it? In the next lesson, we will learn how to fetch and deserialize on-chain account data into a structured object. We'll use the connection we've just established to read information directly from the Solana blockchain and display it in our application, making our dApp truly dynamic.

Can't find a good explanation? Sign up and we'll make it for you

Sign up