1.0.17 • Published 10 months ago

solana-active-wallet-react v1.0.17

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Solana Active Wallet React

useSolanaActiveWallet()

This is a react hook that detects a user's current active solana wallet and returns the public key on the client. It detects when a user has changed their active wallet inside a browser wallet extension and will update and return the active public key accordingly. The wallets currently supported are:

  • Phantom
  • Backpack
  • Solflare
  • TrustWallet
  • Glow

Support for other wallets will be added in the future.

Demo Website

Demo

Installation

npm install solana-active-wallet-react

Note the useSolanaActiveWallet hook relies on the "publicKey" and "wallet" returned by the useWallet hook from the @solana/wallet-adapter-react package. These values are both passed in as parameters when using the hook.

Basic Usage

import useSolanaActiveWallet from "solana-active-wallet-react";
import { useWallet } from "@solana/wallet-adapter-react";

const { publicKey, wallet } = useWallet();
const { activeWalletAddress } = useSolanaActiveWallet(publicKey, wallet);

console.log("active wallet address", activeWalletAddress);

Example Usage

import useSolanaActiveWallet from "solana-active-wallet-react";
import { useWallet } from "@solana/wallet-adapter-react";
import { useWalletModal } from "@solana/wallet-adapter-react-ui";

function ConnectButton() {
  const { publicKey, wallet, connected, disconnect } = useWallet();
  const { slicedWalletAddress } = useSolanaActiveWallet(publicKey, wallet);
  const { setVisible: setModalVisible } = useWalletModal();

  return (
    <button
      className="wallet-connect-button"
      onClick={() => !connected && setModalVisible(true)}
    >
      {connected ? slicedWalletAddress : "Connect Wallet"}
    </button>
  );
}

Returned Values

  • activePublicKey: PublicKey | undefined

The public key of the currently active wallet

  • activeWalletAddress: string | undefined

The wallet address of the currently active wallet

  • slicedWalletAddress: string | null

The wallet address of the currently active wallet sliced for a shortened format eg: 5KgT...gQdY

1.0.17

10 months ago

1.0.16

10 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.12

11 months ago

1.0.11

11 months ago

1.0.10

11 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.1

11 months ago