@safe-global/safe-react-hooks
Version:
A collection of React Hooks that facilitates the interaction of React apps with Safe Smart Accounts
25 lines • 1.14 kB
JavaScript
import { useContext, useEffect, useState } from 'react';
import { SafeContext } from '../SafeContext.js';
import { useCompareObject } from '../hooks/helpers/useCompare.js';
import { createPublicClient } from '../createClient.js';
/**
* Hook to get a SafeClient instance with public capabilities which do not require a signer.
* @param params Parameters to customize the hook behavior.
* @param params.config SafeConfig to use instead of the one provided by `SafeProvider`.
* @returns SafeClient instance or `undefined` if the SafeClient is not initialized yet.
*/
export function usePublicClient(params = {}) {
const { publicClient: publicClientContext } = useContext(SafeContext);
const [publicClient, setPublicClient] = useState();
const hasConfigChanged = useCompareObject(params.config);
useEffect(() => {
if (params.config && hasConfigChanged) {
createPublicClient(params.config).then(setPublicClient);
}
}, [params.config, hasConfigChanged]);
if (params.config) {
return publicClient;
}
return publicClientContext;
}
//# sourceMappingURL=usePublicClient.js.map