@web3-onboard/react
Version:
A collection of React hooks for integrating Web3-Onboard in to React and Next.js projects. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported w
20 lines (19 loc) • 845 B
JavaScript
import { useCallback } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
import { useWeb3Onboard } from '../context.js';
export const useAppState = (stateKey = undefined) => {
const web3Onboard = useWeb3Onboard();
const { select, get } = web3Onboard.state;
const subscribe = useCallback((onStoreChange) => {
const { unsubscribe } = stateKey
? select(stateKey).subscribe(onStoreChange)
: select().subscribe(onStoreChange);
return () => unsubscribe;
}, [stateKey]);
const getSnapshot = useCallback(() => {
const snapshot = get();
return stateKey ? snapshot[stateKey] : snapshot;
}, [stateKey]);
const getServerSnapshot = () => getSnapshot();
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
};