> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# How to Connect a Wallet with React

> Every application on Solana requires a connection with a user's wallet to use. Learn how to connect to wallets on Solana.

Solana's [wallet-adapter](https://github.com/anza-xyz/wallet-adapter) library
makes it easy to manage wallet connections with a frontend application.

## Reference Nextjs Template

This template is only compatible with `@solana/web3.js` (legacy, `1.x`).

<Columns cols={2}>
  <Card
    title="Deploy on Vercel"
    href="https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fsolana-developers%2Fanchor-web3js-nextjs&root-directory=frontend&demo-title=Anchor%20Web3.js%20Next.js%20Demo&demo-description=An%20example%20deployment%20of%20the%20Anchor%20Web3.js%20Next.js%20project.&demo-url=https%3A%2F%2Fanchor-web3js-nextjs.vercel.app%2F&project-name=anchor-web3js-nextjs&repository-name=anchor-web3js-nextjs"
  >
    Deploy your first Solana application on Vercel
  </Card>
  <Card
    title="Vercel Preview"
    href="https://anchor-web3js-nextjs-jade.vercel.app/"
  >
    Try the deployed application
  </Card>
</Columns>

Deploy your first Solana application - a simple Counter Program built using the
Anchor framework. This example covers all the core Solana concepts to get
started building on Solana. View the source code
[here](https://github.com/solana-developers/anchor-web3js-nextjs).

<AccordionGroup>
<Accordion title="Application Demo">

<video
  width="100%"
  autoPlay
  loop
  muted
  playsInline
  style={{
    borderRadius: "10px"
  }}
>
  <source src="/docs-assets/assets/docs/counter.mp4" type="video/mp4" />
  Your browser does not support the video tag.
</video>

</Accordion>
</AccordionGroup>

<Info>
  For a `@solana/kit` example, reference the [Kit
  example](https://github.com/anza-xyz/kit/tree/main/examples/react-app).
</Info>

## Dependencies

<Warning>
  **`@solana/web3.js` 1.x is no longer actively maintained**

  It receives critical fixes only. Use [Kit](/developers/docs/clients/official/javascript#solana-kit) for new code. To migrate existing code, follow the [Kit upgrade guide](https://www.solanakit.com/docs/upgrade-guide), or the [web3.js v3 migration guide](https://github.com/solana-foundation/solana-web3.js/blob/main/docs/web3js-v1-to-v3-migration.md) to keep the class-based API.

  To run this code, install 1.x with `npm install @solana/web3.js@1`. A plain `npm install @solana/web3.js` installs v3, which this code doesn't run on.
</Warning>

If you are creating a new Nextjs project, install the following dependencies:

```bash Terminal
npm install @solana/web3.js@1 \
    @solana/wallet-adapter-base \
    @solana/wallet-adapter-react \
    @solana/wallet-adapter-react-ui
```

<Info>
  This example uses Wallet Standard discovery with `wallets={[]}`, so it does
  not need the `@solana/wallet-adapter-wallets` bundle. That bundle includes
  legacy adapters and can install deprecated WalletConnect v1 dependencies. If
  you need WalletConnect protocol support specifically, use Reown's [Solana
  Adapter](https://docs.reown.com/advanced/providers/solana-adapter).
</Info>

### Create Solana Provider

Create a `SolanaProvider` that will be used to provide the Solana context to the
application.

[Source Reference](https://github.com/solana-developers/anchor-web3js-nextjs/blob/main/frontend/components/counter/provider/Solana.tsx)

```tsx title="Solana Provider"
"use client";

import React, { FC, ReactNode, useMemo } from "react";
import {
  ConnectionProvider,
  WalletProvider
} from "@solana/wallet-adapter-react";
import { WalletAdapterNetwork } from "@solana/wallet-adapter-base";
import { WalletModalProvider } from "@solana/wallet-adapter-react-ui";
import { clusterApiUrl } from "@solana/web3.js";
import "@solana/wallet-adapter-react-ui/styles.css";

interface SolanaProviderProps {
  children: ReactNode;
}

export const SolanaProvider: FC<SolanaProviderProps> = ({ children }) => {
  // The network can be set to 'devnet', 'testnet', or 'mainnet'
  const network = WalletAdapterNetwork.Devnet;

  // You can also provide a custom RPC endpoint
  const endpoint = useMemo(() => clusterApiUrl(network), [network]);

  return (
    <ConnectionProvider endpoint={endpoint}>
      <WalletProvider wallets={[]} autoConnect>
        <WalletModalProvider>{children}</WalletModalProvider>
      </WalletProvider>
    </ConnectionProvider>
  );
};
```

### Wrap the Application in the Solana Provider

Add the `SolanaProvider` to the `RootLayout` in the `app` directory.

[Source Reference](https://github.com/solana-developers/anchor-web3js-nextjs/blob/main/frontend/app/layout.tsx)

```tsx title="Root Layout"
import "./globals.css";
import "@solana/wallet-adapter-react-ui/styles.css";
import { SolanaProvider } from "@/components/counter/provider/Solana";

export default function RootLayout({
  children
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html>
      <body>
        <SolanaProvider>{children}</SolanaProvider>
      </body>
    </html>
  );
}
```

### Add a Connect Button

`WalletMultiButton` opens a modal that lists the wallets the browser has
installed, connects the one the user picks, and then shows the connected
address with options to copy it, change wallet, or disconnect. Read the
connection anywhere inside `SolanaProvider` with `useWallet()`: `publicKey` is
`null` until a wallet connects. Save this component as
`components/WalletButton.tsx`:

```tsx title="Wallet Button"
"use client";

import { useWallet } from "@solana/wallet-adapter-react";
import { WalletMultiButton } from "@solana/wallet-adapter-react-ui";

export function WalletButton() {
  const { publicKey } = useWallet();

  return (
    <div>
      <WalletMultiButton />
      <p>{publicKey ? `Connected: ${publicKey.toBase58()}` : "Not connected"}</p>
    </div>
  );
}
```

Render it on a page inside the provider, for example `app/page.tsx`:

```tsx title="Page"
import { WalletButton } from "@/components/WalletButton";

export default function Home() {
  return (
    <main>
      <WalletButton />
    </main>
  );
}
```
