How to create mui dialog inside dialog in react js?

Hi Friends 👋,

Welcome To aGuideHub!

To create mui dialog inside dialog in react js, you need to put one more dialog inside the component. it will create mui dialog inside dialog in React JS.

Today, I am going to show you, How to create mui dialog inside dialog in react js.

Installation

Install the following packages to use mui dialog in react js.

npm

npm install @mui/material @emotion/react @emotion/styled

yarn

yarn add @mui/material @emotion/react @emotion/styled

Table of contents

  • Install MUI and create a new React app.
  • Import Material-UI dialog.
  • Use the dialog Component

Step 1: Install MUI and create a new React app.

First you have to install the React project. You should use create-react-app command to create a new React project.

npx create-react-app my-app
cd my-app
npm start

Step 2: Import Material-UI dialog.

After installing MUI, you have to import your React component. To do this, add the following line to the top of your component file.

import React, { useState } from "react";
import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import DialogTitle from "@mui/material/DialogTitle";
import DialogContent from "@mui/material/DialogContent";
import DialogActions from "@mui/material/DialogActions";
import IconButton from "@mui/material/IconButton";
import CloseIcon from "@mui/icons-material/Close";

Step 3: Use the dialog Component.

You can use the dialog component in your react js. For example, A Dialog is a type of modal window that appears in front of app content to provide critical information or ask for a decision. Dialogs disable all app functionality when they appear, and remain on screen until confirmed, dismissed, or a required action has been taken. You can use <Dialog> component make mui dialog button in react js.

<Dialog
    open={openOuterDialog}
    onClose={handleCloseOuterDialog}
    maxWidth="sm"
    fullWidth
>
    <DialogTitle>
        Outer Dialog
        <IconButton
            edge="end"
            color="inherit"
            onClick={handleCloseOuterDialog}
            aria-label="close"
            sx={{
              position: "absolute",
              right: 8,
              top: 8
            }}
          >
            <CloseIcon />
          </IconButton>
        </DialogTitle>
        <DialogContent>
          <Button variant="outlined" onClick={handleOpenInnerDialog}>
            Open Inner Dialog
          </Button>
          <Dialog
            open={openInnerDialog}
            onClose={handleCloseInnerDialog}
            maxWidth="sm"
            fullWidth
          >
            <DialogTitle>
              Inner Dialog
              <IconButton
                edge="end"
                color="inherit"
                onClick={handleCloseInnerDialog}
                aria-label="close"
                sx={{
                  position: "absolute",
                  right: 8,
                  top: 8
                }}
              >
                <CloseIcon />
              </IconButton>
            </DialogTitle>
            <DialogContent>
              {/* Content of the inner dialog */}
              {/* Add your inner dialog content here */}
            </DialogContent>
            <DialogActions>
              <Button onClick={handleCloseInnerDialog} color="primary">
                Close
              </Button>
            </DialogActions>
          </Dialog>
        </DialogContent>
        <DialogActions>
          <Button onClick={handleCloseOuterDialog} color="primary">
            Close
        </Button>
    </DialogActions>
</Dialog>    

MUI material create mui dialog inside dialog example.

The below code is an example, you need to import dialog Component. we have two dialogs: the outer dialog and the inner dialog. The handleOpenOuterDialog and handleOpenInnerDialog functions open their respective dialogs, while the handleCloseOuterDialog and handleCloseInnerDialog functions close them. Then it will create mui dialog inside dialog in react js.

App.js

import React, { useState } from "react";
import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import DialogTitle from "@mui/material/DialogTitle";
import DialogContent from "@mui/material/DialogContent";
import DialogActions from "@mui/material/DialogActions";
import IconButton from "@mui/material/IconButton";
import CloseIcon from "@mui/icons-material/Close";

function MyComponent() {
  const [openOuterDialog, setOpenOuterDialog] = useState(false);
  const [openInnerDialog, setOpenInnerDialog] = useState(false);

  const handleOpenOuterDialog = () => {
    setOpenOuterDialog(true);
  };

  const handleCloseOuterDialog = () => {
    setOpenOuterDialog(false);
  };

  const handleOpenInnerDialog = () => {
    setOpenInnerDialog(true);
  };

  const handleCloseInnerDialog = () => {
    setOpenInnerDialog(false);
  };

  return (
    <div>
      <Button variant="outlined" onClick={handleOpenOuterDialog}>
        Open Outer Dialog
      </Button>

      <Dialog
        open={openOuterDialog}
        onClose={handleCloseOuterDialog}
        maxWidth="sm"
        fullWidth
      >
        <DialogTitle>
          Outer Dialog
          <IconButton
            edge="end"
            color="inherit"
            onClick={handleCloseOuterDialog}
            aria-label="close"
            sx={{
              position: "absolute",
              right: 8,
              top: 8
            }}
          >
            <CloseIcon />
          </IconButton>
        </DialogTitle>
        <DialogContent>
          <Button variant="outlined" onClick={handleOpenInnerDialog}>
            Open Inner Dialog
          </Button>
          <Dialog
            open={openInnerDialog}
            onClose={handleCloseInnerDialog}
            maxWidth="sm"
            fullWidth
          >
            <DialogTitle>
              Inner Dialog
              <IconButton
                edge="end"
                color="inherit"
                onClick={handleCloseInnerDialog}
                aria-label="close"
                sx={{
                  position: "absolute",
                  right: 8,
                  top: 8
                }}
              >
                <CloseIcon />
              </IconButton>
            </DialogTitle>
            <DialogContent>
            aGuidehub
              {/* Add your inner dialog content here */}
            </DialogContent>
            <DialogActions>
              <Button onClick={handleCloseInnerDialog} color="primary">
                Close
              </Button>
            </DialogActions>
          </Dialog>
        </DialogContent>
        <DialogActions>
          <Button onClick={handleCloseOuterDialog} color="primary">
            Close
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
}

export default MyComponent;

In the above code example, I have used the @mui/material component and create mui dialog inside dialog in react js.

Check the output of the above code example.

React, mui, dialog, inside, dialog

Here, we are provided code sandbox links for the above program create mui dialog inside dialog in react js. Then you can use whenever you want and do the changes as per your requirements.

Try it Yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts