-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathuseAppKit.ts
More file actions
74 lines (68 loc) · 2.48 KB
/
useAppKit.ts
File metadata and controls
74 lines (68 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { useMemo } from 'react';
import type { ChainNamespace } from '@reown/appkit-common-react-native';
import type { AppKit } from '../AppKit';
import { useAppKitContext } from './useAppKitContext';
/**
* Interface representing the return value of the useAppKit hook
*/
interface UseAppKitReturn {
/** Function to open the AppKit modal with optional view configuration */
open: AppKit['open'];
/** Function to close the AppKit modal */
close: AppKit['close'];
/** Function to disconnect the wallet, optionally scoped to a specific namespace */
disconnect: (namespace?: ChainNamespace) => void;
/** Function to switch to a different network */
switchNetwork: AppKit['switchNetwork'];
}
/**
* Hook to access core AppKit functionality for controlling the modal
*
* @remarks
* This hook provides access to the main AppKit instance methods for opening/closing
* the modal, disconnecting wallets, and switching networks. All functions are memoized
* and properly bound to ensure stable references across renders.
*
* @returns {UseAppKitReturn} An object containing:
* - `open`: Opens the AppKit modal, optionally with a specific view
* - `close`: Closes the AppKit modal
* - `disconnect`: Disconnects the current wallet connection (optionally for a specific namespace)
* - `switchNetwork`: Switches to a different blockchain network
*
* @throws {Error} If used outside of an AppKitProvider
* @throws {Error} If AppKit instance is not available in context
*
* @example
* ```tsx
* function MyComponent() {
* const { open, close, disconnect, switchNetwork } = useAppKit();
*
* return (
* <View>
* <Button onPress={() => open()} title="Connect Wallet" />
* <Button onPress={() => disconnect()} title="Disconnect" />
* <Button
* onPress={() => switchNetwork('eip155:1')}
* title="Switch to Ethereum"
* />
* </View>
* );
* }
* ```
*/
export const useAppKit = (): UseAppKitReturn => {
const context = useAppKitContext();
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 stableFunctions;
};