reactjsnext.jssolanaanchor-solanaphantom-wallet

Wallet get's disconnected from next.js solana application


I have followed a YouTube tutorial to create a solana application, all my code works fine and the wallet gets connected successfull, but when I refresh the page the wallet gets disconnected and the function getWallet returns nothing (connected = false and the PublicKey is null) here's my walletConnectionProvider:

import React, { FC, useMemo } from 'react';
import { ConnectionProvider, WalletProvider } from '@solana/wallet-adapter-react';
import { WalletAdapterNetwork } from '@solana/wallet-adapter-base';
import {
    GlowWalletAdapter,
    PhantomWalletAdapter,
    SlopeWalletAdapter,
    SolflareWalletAdapter,
    SolletExtensionWalletAdapter,
    SolletWalletAdapter,
    TorusWalletAdapter,
} from '@solana/wallet-adapter-wallets';
import {
    WalletModalProvider,
    WalletDisconnectButton,
    WalletMultiButton
} from '@solana/wallet-adapter-react-ui';
import { clusterApiUrl } from '@solana/web3.js';

require('@solana/wallet-adapter-react-ui/styles.css');

export const WalletConnectionProvider = ({children}) => {
    const network = WalletAdapterNetwork.Devnet;

    const endpoint = useMemo(() => clusterApiUrl(network), [network]);

    const wallets = useMemo(
        () => [
            new PhantomWalletAdapter(),
            new GlowWalletAdapter(),
            new SlopeWalletAdapter(),
            new SolflareWalletAdapter({ network }),
            new TorusWalletAdapter(),
        ],
        [network]
    );

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

export default WalletConnectionProvider

thanks


Solution

  • You have to add AutoConnectProvider, create a file called AutoConnectProvider and add this code

    import { useLocalStorage } from '@solana/wallet-adapter-react';
    import { createContext, FC, ReactNode, useContext } from 'react';
    
    export interface AutoConnectContextState {
        autoConnect: boolean;
        setAutoConnect(autoConnect: boolean): void;
    }
    
    export const AutoConnectContext = createContext<AutoConnectContextState>({} as AutoConnectContextState);
    
    export function useAutoConnect(): AutoConnectContextState {
        return useContext(AutoConnectContext);
    }
    
    export const AutoConnectProvider: FC<{ children: ReactNode }> = ({ children }:any) => {
        // TODO: fix auto connect to actual reconnect on refresh/other.
        // TODO: make switch/slider settings
        // const [autoConnect, setAutoConnect] = useLocalStorage('autoConnect', false);
        const [autoConnect, setAutoConnect] = useLocalStorage('autoConnect', true);
    
        return (
            <AutoConnectContext.Provider value={{ autoConnect, setAutoConnect }}>{children}</AutoConnectContext.Provider>
        );
    };
    

    then in whichever file you have your WalletContext wrap it with the AutoContextProvider

    export const ContextProvider: FC<{ children: ReactNode }> = ({ children }) => {
        return (
            <AutoConnectProvider>
                <WalletContextProvider>{children}</WalletContextProvider>
            </AutoConnectProvider>
        )
    }
    

    Hopw this helps :)