【问题标题】:React Dialog box closed issueReact 对话框关闭问题
【发布时间】:2020-06-21 03:16:33
【问题描述】:

我想使用材质 UI 的对话框。我正在使用另一个组件从右侧菜单(侧边栏-> 用户注册)导航该对话框。我只想在当前主页或关于我们的页面上打开那个对话框。这是一个用户注册对话框。你能帮忙吗?

当我试图打开用户注册对话框时,我无法在当前页面打开对话框,这就是为什么我为对话框组件创建了一个单独的组件。

我想在选择侧边菜单选项时打开该对话框。该对话框应在当前页面中打开。

这是代码沙箱链接。 https://codesandbox.io/s/immutable-sound-ggj4w

应用js

import React from "react";
import "./styles.css";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import Home from "./home";
import About from "./about";
import Dialog from "./dialog";
import SideMenu from "./sidemu";
export default function App() {
  return (
    <div className="App">
      <BrowserRouter>

      <SideMenu />
        {/* <Switch> */}
          <Route exact path="/" component={Home} />
          <Route exact path="/about" component={About} />
          <Route exact path="/sidemenu" component={SideMenu} />
          <Route exact path="/dialog" component={Dialog} />
        {/* </Switch> */}
      </BrowserRouter>
      {/* <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2> */}
    </div>
  );
}

主页 js

import React, { Component } from "react";

class Home extends Component {
  render() {
    return (
      <div>
        <div>Home</div>
      </div>
    );
  }
}

export default Home;

关于我们

import React, { Component } from "react";

class Home extends Component {
  render() {
    return (
      <div>
        <div>about us</div>
      </div>
    );
  }
}

export default Home;

对话框js

import React from 'react';
import Button from '@material-ui/core/Button';
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';
import DialogTitle from '@material-ui/core/DialogTitle';

export default function AlertDialog() {
  const [open, setOpen] = React.useState(false);

  const handleClickOpen = () => {
    setOpen(true);
  };

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <div>
      <Button variant="outlined" color="primary" onClick={handleClickOpen}>
        Open alert dialog
      </Button>
      <Dialog
        open={true}
        onClose={handleClose}
        aria-labelledby="alert-dialog-title"
        aria-describedby="alert-dialog-description"
      >
        <DialogTitle id="alert-dialog-title">{"Use Google's location service?"}</DialogTitle>
        <DialogContent>
          <DialogContentText id="alert-dialog-description">
            Let Google help apps determine location. This means sending anonymous location data to
            Google, even when no apps are running.
          </DialogContentText>
        </DialogContent>
        <DialogActions>
          <Button onClick={handleClose} color="primary">
            Disagree
          </Button>
          <Button onClick={handleClose} color="primary" autoFocus>
            Agree
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
}

sidemenu js

import React from 'react';
import './styles.css';
import { Link } from 'react-router-dom';
import { makeStyles } from '@material-ui/core/styles';
import Drawer from '@material-ui/core/Drawer';
import { List, ListItemIcon, ListItem, ListItemText } from '@material-ui/core';
import MenuRoundedIcon from '@material-ui/icons/MenuRounded';
import HomeRoundedIcon from '@material-ui/icons/HomeRounded';
import MenuBookRoundedIcon from '@material-ui/icons/MenuBookRounded';

const useStyles = makeStyles({
  list: {
    width: 250,
  },
  fullList: {
    width: 'auto',
  },
});

export default function TemporaryDrawer() {
  const classes = useStyles();
  const [state, setState] = React.useState({
    top: false,
    left: false,
    bottom: false,
    right: false,
  });

  const toggleDrawer = (side, open) => event => {
    if (event.type === 'keydown' && (event.key === 'Tab' || event.key === 'Shift')) {
      return;
    }

    setState({ ...state, [side]: open });
  };

  const sideList = side => (
    <div
      className={classes.list}
      role="presentation"
      onClick={toggleDrawer(side, false)}
      onKeyDown={toggleDrawer(side, false)}
    >
      <List>
        <Link className="right-menu-data" to="/">
          <ListItem button>
            <ListItemIcon>
              <HomeRoundedIcon />
            </ListItemIcon>
            <ListItemText>Home</ListItemText>
          </ListItem>
        </Link>
        <Link className="right-menu-data" to="/about"><ListItem button>
          <ListItemIcon>
            <MenuBookRoundedIcon />
          </ListItemIcon>
          <ListItemText>About us</ListItemText>
        </ListItem>
        </Link>
        <Link className="right-menu-data" to="/dialog"><ListItem button>
          <ListItemIcon>
            <MenuBookRoundedIcon />
          </ListItemIcon>
          <ListItemText>User Registration</ListItemText>
        </ListItem>
        </Link>
      </List>


    </div>
  );


  return (
    <div>
      <MenuRoundedIcon className="careerpedia-menu-bars" onClick={toggleDrawer('right', true)} />
      <Drawer anchor="right" open={state.right} onClose={toggleDrawer('right', false)}>
        {sideList('right')}
      </Drawer>
    </div>
  );
}

【问题讨论】:

  • 你能解释一下你真正想要实现的目标吗
  • 我想使用诸如(家长注册、学生注册)等选项的侧边菜单打开对话框(用户注册表单)。该对话框应该在当前网址中...

标签: reactjs


【解决方案1】:

实现此目的的一种方法是将对话框组件中打开/关闭模式的逻辑分解到“中心”位置,最好是在应用程序中,但在路由器之外。这允许对话框通过可在应用程序周围传递的回调从单个位置打开/关闭,并且不会耦合到您的应用程序所在的任何特定路线

快速重构您的代码:

dialog.js

const AlertDialog = ({ open, onAgree, onClose, onDisagree }) => (
  <Dialog
    open={open}
    onClose={onClose}
    aria-labelledby="alert-dialog-title"
    aria-describedby="alert-dialog-description"
  >
    <DialogTitle id="alert-dialog-title">
      {"Use Google's location service?"}
    </DialogTitle>
    <DialogContent>
      <DialogContentText id="alert-dialog-description">
        Let Google help apps determine location. This means sending anonymous
        location data to Google, even when no apps are running.
      </DialogContentText>
    </DialogContent>
    <DialogActions>
      <Button onClick={onDisagree} color="primary">
        Disagree
      </Button>
      <Button onClick={onAgree} color="primary" autoFocus>
        Agree
      </Button>
    </DialogActions>
  </Dialog>
);

App.js

export default function App() {
  const [open, setOpen] = useState(false);

  const openDialog = () => setOpen(true);
  const closeDialog = () => setOpen(false);

  const onAgreeHandler = () => {
    closeDialog();
    alert("AGREED");
  };
  const onDisgreeHandler = () => {
    closeDialog();
    alert("DISAGREED");
  };
  const onCloseHandler = (event, reason) => {
    closeDialog();
    alert(`Dialog closed. Reason: ${reason}`);
  };

  return (
    <div className="App">
      <BrowserRouter>
        <SideMenu openDialog={openDialog} /> // Pass dialog open callback to sidemenu
        <Route exact path="/" component={Home} />
        <Route exact path="/about" component={About} />
        <Route exact path="/dialog" component={Dialog} />
      </BrowserRouter>

      <Button variant="outlined" color="primary" onClick={openDialog}>
        Open alert dialog test
      </Button>

      <Dialog
        open={open}
        onAgree={onAgreeHandler}
        onClose={onCloseHandler}
        onDisagree={onDisgreeHandler}
      />
    </div>
  );
}

sidemu.js

export default function TemporaryDrawer({ openDialog }) { // Accept openDialog callback

  ...
    { /* Add new menu list item (not in a Link!) */ }
    <ListItem button onClick={openDialog}> // Set onClick handler to openDialog callback
      <ListItemIcon>
        <MenuBookRoundedIcon />
      </ListItemIcon>
      <ListItemText>Trigger dialog?</ListItemText>
    </ListItem>

注意:随着您的应用程序在大小/复杂性方面的增长,这种模式可能会变得有点笨拙(如果您需要通过任何其他后代打开对话框)并且您可能希望切换到使用专门用于对话框的反应上下文提供者/消费者,或者使用像 Redux 这样的状态管理系统,以便您可以调度简单的操作来打开/关闭它。

【讨论】:

  • 非常感谢....没有警告框有没有可能工作...
  • 对不起...我在stackover flow中又问了一个问题.....请也看看这个.....stackoverflow.com/questions/60636396/…............ .....
  • @ManuPanduu 是的,你可以随心所欲。那只是为了演示如何处理与对话框交互的每个场景。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 2010-10-26
  • 1970-01-01
  • 1970-01-01
  • 2011-01-16
相关资源
最近更新 更多