ReactJS 模态 Material UI

ReactJS Modal with Material UI

我正在尝试使用 Material UI 创建一个可重复使用的确认模式,但是当我按下取消或确定时,模式不会关闭。

代码在这里:

https://codesandbox.io/s/lucid-hoover-sput6?file=/src/App.js

我不明白为什么流行音乐不会消失。

LE:在此处添加了代码,因此它仍然存在

ConfirmModal.js

import React from "react";
import { Button } from "@material-ui/core";
import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";

const ConfirmModal = (props) => {
  const { content, open, setOpen, onConfirm } = props;

  return (
    <Dialog
      open={open}
      onClose={() => setOpen(false)}
      aria-labelledby="dialog-title"
    >
      <DialogContent>
        <DialogContentText>{content}</DialogContentText>
      </DialogContent>
      <DialogActions>
        <Button autoFocus onClick={() => setOpen(false)} color="primary">
          Cancel
        </Button>
        <Button
          onClick={() => {
            setOpen(false);
            onConfirm();
          }}
          color="primary"
        >
          OK
        </Button>
      </DialogActions>
    </Dialog>
    // </div>
  );
};

export default ConfirmModal;

App.js

import React, { useState } from "react";
import { IconButton } from "@material-ui/core";
import { Input as InputIcon } from "@material-ui/icons";

import ConfirmModal from "./ConfirmModal";

export default function App() {
  const [confirmOpen, setConfirmOpen] = useState(false);

  const handleLogout = () => {
    console.log("this hould logout the user");
  };

  return (
    <div className="App">
      <h2>Press the button below so the confirmation modal appears </h2>
      <IconButton color="inherit" onClick={() => setConfirmOpen(true)}>
        <InputIcon />
        <ConfirmModal
          content="Are you sure you want to leeeave us ?"
          open={confirmOpen}
          setOpen={setConfirmOpen}
          onConfirm={handleLogout}
        />
      </IconButton>
    </div>
  );
}

将模式移出按钮。 Modal 的 cancel/confirm/backdrop 单击事件传播(冒泡)到打开按钮 (IconButton) 并且它的 onClick 处理程序只是通过设置 confirmOpen 状态 [=14] 重新打开模态=].

export default function App() {
  const [confirmOpen, setConfirmOpen] = useState(false);

  const handleLogout = () => {
    console.log("this hould logout the user");
  };

  return (
    <div className="App">
      <h2>Press the button below so the confirmation modal appears </h2>
      <IconButton color="inherit" onClick={() => setConfirmOpen(true)}>
        <InputIcon />
      </IconButton>
      <ConfirmModal
        content="Are you sure you want to leeeave us ?"
        open={confirmOpen}
        setOpen={setConfirmOpen}
        onConfirm={handleLogout}
      />
    </div>
  );
}