Prerequisites
This guide assumes you are using React, and have a working React application. To set up a new React app, run:For additional front-end framework support, visit the Solana wallet adapter documentation to find more starter projects and packages.
Install
Install the Solana JavaScript API:Step 1: Create a new component
Create a new file namedWalletAdapter.js that defines a new WalletAdapter React component that renders the components of your app.
We use this component to set up the adapters for each Solana wallet we want to support.
Step 2: Add UI components and styling
The Solana wallet adapter library provides UI components for common UI frameworks. For this guide, we use the basic React package for styling (no UI framework). In yourWalletAdapter.js file, import the WalletModalProvider and the default CSS styles from the @solana/wallet-adapter-react-ui package. Wrap the WalletModalProvider around your app’s components.
For additional UI framework support, such as Material UI and Ant Design, visit the Solana wallet adapter documentation.
Step 3: Configure wallets
To configure which wallets you want to support in your application, use theWalletProvider component.
In your WalletAdapter.js file, import the WalletProvider component from the @solana/wallet-adapter-react package. Wrap the WalletProvider component around the WalletModalProvider component.
WalletProvider component accepts a list of wallets as a prop. To add wallets, import adapters from the @solana/wallet-adapter-wallets package for each wallet you want to support, and pass a list of the wallet adapters as a prop into the WalletProvider component.
You can also specify the
autoconnect attribute on the WalletProvider component to automatically attempt to reconnect a user wallet upon a page refresh.Step 4: Configure network connection
To configure which network you want wallets to establish a connection to, use theConnectionProvider component.
In your WalletAdapter.js file, import the ConnectionProvider component from the @solana/wallet-adapter-react package. Wrap the ConnectionProvider component around the WalletProvider component.
ConnectionProvider component accepts a network endpoint as a prop. You can set your network to devnet, testnet, mainnet-beta, or provide a custom RPC endpoint.
Step 5: Add wallet connection button
To connect and disconnect user wallets, use theWalletConnectButton and WalletDisconnectButton components respectively, or the WalletMultiButton component which enables connecting and disconnecting using a single button. For this guide, we use WalletMultiButton.
In your App.js file, import the WalletMultiButton component from the @solana/wallet-adapter-react-ui package. Include the WalletMultiButton component alongside the other components of your app.
Step 5: Use React hooks
Once your application has a button that enables users to connect their wallets, you can use theuseWallet() React hook to get a user’s public key, check connection status of their wallet, as well as sign messages and send transactions on their behalf.
In your App.js file, import the userWallet React hook from the @solana/wallet-adapter-react package, and use it to access the user’s public key and other methods like sendTransaction.