Skip to content

requestContact ​

This custom React hook opens the native contact picker and returns the selected contact's name and address. It tracks the loading state and handles errors.

Code Example ​

Below is a custom hook that wraps the minipay_requestContact RPC call:

typescript
type Contact = { name: string; address: string };

const useRequestContact = (config: Config) => {
  const [contact, setContact] = useState<Contact | null>(null);
  const [error, setError] = useState<Error | null>(null);
  const [isPending, setIsPending] = useState(false);
  const { client } = useMiniPayClient(config);

  return {
    contact,
    error,
    isPending,
    async requestContact(): Promise<void> {
      if (!client) return;
      setError(null);
      setIsPending(true);
      setContact(null);

      try {
        const result = await client.request({
          method: "minipay_requestContact",
          params: [],
        });
        setContact(result);
      } catch (e: unknown) {
        if (e instanceof Error) {
          setError(e);
        }
      } finally {
        setIsPending(false);
      }
    },
  };
};

Simple usage example:

typescript
const { requestContact, contact, isPending, error } = useRequestContact(config);

return (
  <button onClick={() => requestContact()}>
    Pick Contact
  </button>
);