Skip to content

useDialog

import { useDialog } from 'react-dialog-async';

This hook is used to manage an instance of the given dialog component. It returns methods to open & close the dialog.

It must be called within a <DialogProvider/>.

const myDialog = useDialog(MyDialog, {
// pass options here
});
const handleClick = async () => {
const result = await myDialog.open({
// pass data to dialog here
});
if (result) {
// handle the result from the dialog
}
}
function useDialog<D, R, DE extends D | undefined>(
component: AsyncDialogComponent<D, R>,
options?: useDialogOptions<D, DE>,
): useDialogReturn<D, R, DE>
ParameterTypeDefaultDescription
componentAsyncDialogComponent-The dialog component to be rendered.
optionsuseDialogOptions{}Optional configuration options.

A React component that accepts props of type AsyncDialogProps. See AsyncDialogProps for details.

PropertyTypeDefaultDescription
defaultDataDE-Default data to be passed to the dialog when opened without data
unmountDelayInMsnumber-When the dialog is closed, it will wait this long until it is unmounted. Setting this overrides any globally configured values
persistOnUnmountbooleanfalseIf true, the dialog will not be closed when the hook unmounts
PropertyTypeDescription
open(data: D) => Promise<R | undefined>Opens the dialog, and passes the provided data as props to the dialog component. Returns a promise that resolves when the dialog is closed
close() => voidForce closes the dialog
updateData(data: D) => voidUpdates the data passed to the dialog component.

View on GitHub