40
loading...
This website collects cookies to deliver better user experience
confirm/cancel
dialog windows but have you ever tried implementing it in your React application? Are you sure?
Context API
instead but Zustand seems cleaner for this project, in my humble opinion!npm install @material-ui/core @material-ui/icons zustand
// ConfirmDialog.jsx
// material ui
import {
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Button,
Box,
IconButton,
Typography,
} from '@material-ui/core';
import { Close } from '@material-ui/icons';
const ConfirmDialog = () => {
return (
<Dialog open={true} maxWidth="sm" fullWidth>
<DialogTitle>Confirm the action</DialogTitle>
<Box position="absolute" top={0} right={0}>
<IconButton>
<Close />
</IconButton>
</Box>
<DialogContent>
<Typography>some message here</Typography>
</DialogContent>
<DialogActions>
<Button color="primary" variant="contained">
Cancel
</Button>
<Button color="secondary" variant="contained">
Confirm
</Button>
</DialogActions>
</Dialog>
);
};
export default ConfirmDialog;
App.js
. This will make the component reachable from all over your project. You can see your component rendered on the screen like this: Most probably your colors will not be the same because of my material UI theme configuration but the functionality will be exactly the same.
create
method from Zustand first.// ConfirmDialog.jsx
// ...
import create from 'zustand';
// ...
// ConfirmDialog.jsx
// ...
const useConfirmDialogStore = create((set) => ({
message: '',
onSubmit: undefined,
close: () => set({ onSubmit: undefined }),
}));
// ...
const ConfirmDialog2 = () => {
// destructure the store data and functions
const { message, onSubmit, close } = useConfirmDialogStore();
return (
// if the onSubmit is undefined the dialog will be closed.
// close() function sets the onSubmit to undefined,
// so it will close the dialog, if we pass it to the onClose attribute.
<Dialog open={Boolean(onSubmit)} onClose={close} maxWidth="sm" fullWidth>
<DialogTitle>Confirm the action</DialogTitle>
<Box position="absolute" top={0} right={0}>
<IconButton onClick={close}>
<Close />
</IconButton>
</Box>
<DialogContent>
<Typography>{message}</Typography>
</DialogContent>
<DialogActions>
<Button color="primary" variant="contained" onClick={close}>
Cancel
</Button>
<Button
color="secondary"
variant="contained"
onClick={() => {
if (onSubmit) {
onSubmit();
}
close();
}}
>
Confirm
</Button>
</DialogActions>
</Dialog>
);
};
// ConfirmDialog.jsx
// ...
export const confirmDialog = (message, onSubmit) => {
useConfirmDialogStore.setState({
message,
onSubmit,
});
};
// ...
ConfirmDialog.jsx
component so it will be available in the other parts of the application. Delete All The Data
. I bet you you want to warn the user before implementing this action!// App.tsx
import { confirmDialog } from './ConfirmDialog';
// ...
<Button
onClick={() => {
confirmDialog('Do you really want to delete all the data?', () =>
console.log('deleting all the data!')
);
}}
>
Delete All The Data
</Button>;
confirm
button!React
, Hasura
, Auth0
, and Apollo
. I will be sharing my challenges and the solutions to these challenges here while creating the actual project.