Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[feature] Is it possible for useAppkit to expose the full appKit instance? #485

@Holybasil

Description

@Holybasil

What problem does this new feature solve?

The switchNetwork function requires an AppKitNetwork, but the networks exported from '@reown/appkit/networks' are not in the AppKitNetwork format.

I noticed that AppKit internally formats these networks and attaches them to this.networks, but unfortunately, this data is not exported.

As a result, I have to manually reformat the imported network from '@reown/appkit/networks' like this

appKitNetwork: { ...arbitrum, chainNamespace: 'eip155', caipNetworkId: 'eip155:42161' }

This approach is less than ideal and feels a bit clunky. I’m opening this issue to propose exporting the appKit instance, so developers can directly access the data and functions it provides.

Describe the solution you'd like

export const useAppKit = (): UseAppKitReturn => {
  const context = useContext(AppKitContext);

  if (context === undefined) {
    throw new Error('useAppKit must be used within an AppKitProvider');
  }

  if (!context.appKit) {
    // This might happen if the provider is rendered before AppKit is initialized
    throw new Error('AppKit instance is not yet available in context.');
  }

  const { appKit } = context;

  const stableFunctions = useMemo(() => {
    if (!context.appKit) {
      throw new Error('AppKit instance is not available');
    }

    return {
      open: context.appKit.open.bind(context.appKit),
      close: context.appKit.close.bind(context.appKit),
      disconnect: (namespace?: ChainNamespace) =>
        context.appKit!.disconnect.bind(context.appKit!)(namespace),
      switchNetwork: context.appKit.switchNetwork.bind(context.appKit),


    };
  }, [context.appKit]);

  return { appkit, ...stableFunctions };
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions