@web3-react/core#getSelectedConnector TypeScript Examples

The following examples show how to use @web3-react/core#getSelectedConnector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Web3ReactProvider.tsx    From anchor-web-app with Apache License 2.0 5 votes vote down vote up
export function Web3ReactProvider<T extends BaseProvider = Web3Provider>(
  props: Web3ReactProviderProps,
) {
  const { children } = props;

  const [connectionType, setConnectionType] = useLocalStorage<ConnectType>(
    '__anchor_evm_wallet_connect_type__',
    ConnectType.None,
  );

  const {
    useSelectedChainId,
    useSelectedAccounts,
    useSelectedIsActivating,
    useSelectedError,
    useSelectedAccount,
    useSelectedIsActive,
    useSelectedProvider,
  } = getSelectedConnector(...connectors);

  const [connector, , store] = getWeb3Connector(connectionType);

  const chainId = useSelectedChainId(connector);
  const accounts = useSelectedAccounts(connector);
  const isActivating = useSelectedIsActivating(connector);
  const error = useSelectedError(connector);
  const account = useSelectedAccount(connector);
  const isActive = useSelectedIsActive(connector);
  const provider = useSelectedProvider<T>(connector, 'any');

  const connect = useCallback(
    (connectionType: ConnectType) => {
      setConnectionType(connectionType);
      return getWeb3Connector(connectionType)[0];
    },
    [setConnectionType],
  );

  const disconnect = useCallback(() => {
    setConnectionType(ConnectType.None);
  }, [setConnectionType]);

  const value = useMemo(() => {
    return {
      chainId,
      accounts,
      isActivating,
      error,
      account,
      isActive,
      provider,
      connector,
      connectionType: connectionType ?? ConnectType.None,
      store,
      connect,
      disconnect,
    };
  }, [
    chainId,
    accounts,
    isActivating,
    error,
    account,
    isActive,
    provider,
    connector,
    connectionType,
    store,
    connect,
    disconnect,
  ]);

  return <Web3Context.Provider value={value}>{children}</Web3Context.Provider>;
}