-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[dialog] Add imperative methods to display them directly #24759
Comments
Could you expand a bit on how you would use it and why the existing API cannot be used for that use case? Offering an imperative API for state updates can be quite problematic since we or React is no longer in control of the semantics of such an update. So these APIs impose quite the maintenance burden and it's not clear why it is needed considering this is a React library. |
Example: https://ant.design/components/modal/ In many scenarios, the notification dialog needs to be called repeatedly to avoid introducing too many |
Could you describe these scenarios in detail? I'm not following why this API is required to display a single dialog instead of multiple ones. |
To achieve scenario you can be use library such as toastify or notistack. |
Thanks for opening this issue. I thought that we already had an open issue for this pain point. The equivalent issue for the Snackbar is #18098. I have updated the issue's description with the benchmark I have been doing on this for a year or two (I didn't find much). |
@CarbonPool most likely you want imperative handler(s) to be set on the instance of the dialog component rather than relying on static methods, for example: import * as React from "react";
import { Button, Container } from "@mui/material";
import { ConfirmDialog, DialogElement } from "./ConfirmDialog.js";
export function Example(): JSX.Element {
const dialogRef = React.useRef<DialogElement>(null);
return (
<Container>
<Button onClick={() => dialogRef.current?.open()}>Show Dialog</Button>
<ConfirmDialog ref={dialogRef} onConfirm={...} />
</Container>
);
} @oliviertassinari BTW, it might be a good idea to add kriasoft/react-starter-kit#2004 Alternatively, with import * as React from "react";
import { Button, Container } from "@mui/material";
import { useDialog } from "./core/dialog.js";
import { ConfirmDialog } from "./dialogs/ConfirmDialog.js"; // Custom dialog based on Material UI <Dialog />
export function Example(): JSX.Element {
const dialog = useDialog(
// factory method returning an instance of Material UI dialog
() => <ConfirmDialog onConfirm={...} />, // `open`, and `onClose` props will be added by the hook
[] // React hook dependencies
);
return (
<Container>
<Button onClick={dialog.show}>Show Dialog</Button>
</Container>
);
} By default, the Alternatively, the import * as React from "react";
import { Button, Container } from "@mui/material";
import { useDialog } from "./core/dialog.js";
import { ConfirmDialog } from "./dialogs/ConfirmDialog.js"; // Custom dialog based on Material UI <Dialog />
export function Example(): JSX.Element {
const dialog = useDialog(
// factory method returning an instance of Material UI dialog
() => <ConfirmDialog onConfirm={...} />, // `open`, and `onClose` props will be added by the hook
[] // React hook dependencies
);
return (
<Container>
<Button onClick={dialog.show}>Show Dialog</Button>
{dialog.component}
</Container>
);
} |
The Toolpad team is exploring imperative APIs for interacting with dialogs. If this is something that interests you, I invite you to read and comment on the RFC. Toolpad Core is new set of abstractions we're creating to help you build internal and B2B applications faster |
It's now live in https://mui.com/toolpad/core/react-use-dialogs/. |
Refer to the call and implementation of the
ant-design
dialog box. This solution brings more flexibility and concise code.Will there be such a solution?
Benchmark
The text was updated successfully, but these errors were encountered: