Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

WalletContextState is not getting updated, so cannot get any value like publicKey or connected #73

Closed
nukec opened this issue Sep 14, 2021 · 5 comments

Comments

@nukec
Copy link

nukec commented Sep 14, 2021

I successfully connect to the wallet:
image

Code is below, but publicKey is not getting updated after I am connected so useEffects is not triggered aka I am not getting anything from useWallet:

import {
  WalletError,
} from "@solana/wallet-adapter-base";
import {
  ConnectionProvider,
  useLocalStorage,
  useWallet,
  WalletProvider,
} from "@solana/wallet-adapter-react";
import {
  getBloctoWallet,
  getLedgerWallet,
  getPhantomWallet,
  getSlopeWallet,
  getSolflareWallet,
  getSolletExtensionWallet,
  getSolletWallet,
} from "@solana/wallet-adapter-wallets";
import { useSnackbar } from "notistack";
import { useCallback, useEffect, useMemo } from "react";
import { useRecoilState, useRecoilValue } from "recoil";
import {
  atomClusterApiUrl,
  atomIsWalletConnected,
  atomNetwork,
} from "../../recoil/atoms";

function WalletSolana() {
  const network = useRecoilValue(atomNetwork);
  const clusterUrl = useRecoilValue(atomClusterApiUrl);

  const { publicKey, connected } = useWallet();

  const [isWalletConnected, setWalletConnected] = useRecoilState(
    atomIsWalletConnected
  );

  const [autoConnect, setAutoConnect] = useLocalStorage("autoConnect", false);

  const wallets = useMemo(
    () => [
      getPhantomWallet(),
      getSolflareWallet(),
      getSlopeWallet(),
      getLedgerWallet(),
      getBloctoWallet(),
      getSolletWallet(),
      getSolletExtensionWallet(),
    ],
    [network]
  );

  const { enqueueSnackbar } = useSnackbar();

  const onError = useCallback(
    (error: WalletError) => {
      enqueueSnackbar(
        error.message ? `${error.name}: ${error.message}` : error.name,
        { variant: "error" }
      );
      console.error(error);
    },
    [enqueueSnackbar]
  );

  useEffect(() => {
    if (publicKey) {
      console.log("got public key " + publicKey);
    }
  }, [publicKey]);

  return (
    <div>
      <ConnectionProvider endpoint={clusterUrl}>
        <WalletProvider
          wallets={wallets}
          onError={onError}
          autoConnect={autoConnect}
        >
          <ReactUIWalletModalProvider>
            <ReactUIWalletMultiButton />
          </ReactUIWalletModalProvider>
        </WalletProvider>
      </ConnectionProvider>
    </div>
  );
}

export default WalletSolana;
@nukec nukec changed the title I am not getting publicKey after successfully selected wallet and connected WalletContextState is not getting updated, so cannot get any value like publicKey or connected Sep 14, 2021
@nukec
Copy link
Author

nukec commented Sep 14, 2021

This has been resolved by wrapping the app with context.

@nukec nukec closed this as completed Sep 14, 2021
@jordaaash
Copy link
Collaborator

Yeah, you need to wrap the app -- or more specifically, any component subtree you expect to call the useWallet hook from -- with the context. Glad you figured out out!

@DadAndSonCartoon
Copy link

Thanks for the questions and answers. I was briefly stuck on this.

@justinbalaguer
Copy link

can somebody give me an example on how should I wrap my component with context?

@jordaaash
Copy link
Collaborator

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants