首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >React & MUI:移动视图上的导航

React & MUI:移动视图上的导航
EN

Stack Overflow用户
提问于 2021-02-07 08:24:11
回答 1查看 1.6K关注 0票数 0

我在YouTube上找到了一个用抽屉创建导航的例子,并在这里创建了一个代码沙箱:https://codesandbox.io/s/musing-hofstadter-rj4vn?file=/src/App.js

我需要维护大屏幕的两个窗格设计,而对于移动视图,每次只需要显示一个窗格:

  • 当应用程序第一次加载时-显示菜单
  • 单击链接时-显示内容
  • 当单击内容页上的“后退”按钮时-显示菜单

Web视图:

移动视图:

  • 默认情况下显示菜单:

  • 单击链接时,显示内容。用户可以单击Back按钮返回菜单

我注意到我可以将抽屉的open值设置为true或false。但是当我这样做的时候,屏幕的左边仍然有空白,抽屉应该在那里。

我找到了一个参考文献来隐藏移动屏幕上的抽屉,但这对我没有帮助,因为我确实需要在应用程序第一次加载时,然后当用户单击内容页面上的后退按钮时,显示抽屉。

我怎样才能做到这一点?任何帮助都是非常感谢的。谢谢。

代码:

代码语言:javascript
运行
复制
import React from "react";
import { makeStyles } from "@material-ui/core/styles";

import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";

import {
  Button,
  Drawer,
  List,
  ListItem,
  ListItemIcon,
  ListItemText,
  Container,
  Typography
} from "@material-ui/core";

import HomeIcon from "@material-ui/icons/Home";
import InfoIcon from "@material-ui/icons/Info";

const useStyles = makeStyles((theme) => ({
  button: {
    margin: theme.spacing(1)
  },
  drawerPaper: { width: "inherit" },
  link: {
    textDecoration: "none",
    color: theme.palette.text.primary
  }
}));

function App() {
  const classes = useStyles();
  return (
    <Router>
      <div style={{ display: "flex" }}>
        <Drawer
          style={{ width: "220px" }}
          variant="persistent"
          anchor="left"
          open={true}
          classes={{ paper: classes.drawerPaper }}
        >
          <List>
            <Link to="/" className={classes.link}>
              <ListItem button>
                <ListItemIcon>
                  <HomeIcon />
                </ListItemIcon>
                <ListItemText primary={"Home"} />
              </ListItem>
            </Link>
            <Link to="/about" className={classes.link}>
              <ListItem button>
                <ListItemIcon>
                  <InfoIcon />
                </ListItemIcon>
                <ListItemText primary={"About"} />
              </ListItem>
            </Link>
          </List>
        </Drawer>
        <Switch>
          <Route exact path="/">
            <Container>
              <Button
                variant="contained"
                color="secondary"
                className={classes.button}
                component={Link}
                to="/"
              >
                Back
              </Button>
              <Typography variant="h3" gutterBottom>
                Home
              </Typography>
              <Typography variant="body1" gutterBottom>
                Lorem Ipsum is simply dummy text of the printing and typesetting
                industry. Lorem Ipsum has been the industry's standard dummy
                text ever since the 1500s, when an unknown printer took a galley
                of type and scrambled it to make a type specimen book. It has
                survived not only five centuries, but also the leap into
                electronic typesetting, remaining essentially unchanged. It was
                popularised in the 1960s with the release of Letraset sheets
                containing Lorem Ipsum passages, and more recently with desktop
                publishing software like Aldus PageMaker including versions of
                Lorem Ipsum.
              </Typography>
            </Container>
          </Route>
          <Route exact path="/about">
            <Container>
              <Button
                variant="contained"
                color="secondary"
                className={classes.button}
                component={Link}
                to="/"
              >
                Back
              </Button>
              <Typography variant="h3" gutterBottom>
                About
              </Typography>
              <Typography variant="body1" gutterBottom>
                Contrary to popular belief, Lorem Ipsum is not simply random
                text. It has roots in a piece of classical Latin literature from
                45 BC, making it over 2000 years old. Richard McClintock, a
                Latin professor at Hampden-Sydney College in Virginia, looked up
                one of the more obscure Latin words, consectetur, from a Lorem
                Ipsum passage, and going through the cites of the word in
                classical literature, discovered the undoubtable source. Lorem
                Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
                Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero,
                written in 45 BC. This book is a treatise on the theory of
                ethics, very popular during the Renaissance. The first line of
                Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
                in section 1.10.32.
              </Typography>
            </Container>
          </Route>
        </Switch>
      </div>
    </Router>
  );
}

export default App;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-07 09:06:17

我认为资料界面的useMediaQuery可能会有帮助。在主路径( Drawer )上的Route中呈现"/"组件,并在某些媒体断点下有条件地设置exact支柱(如果路由应该匹配或不匹配)。

示例:

代码语言:javascript
运行
复制
...
import useMediaQuery from '@material-ui/core/useMediaQuery';

...

function App() {
  const classes = useStyles();
  const isMobileMatch = useMediaQuery("(max-width:600px)"); // <-- set breakpoint

  return (
    <Router>
      <div style={{ display: "flex" }}>
        <Route exact={isMobileMatch} path="/"> // <-- conditionally match exactly
          <Drawer
            style={{ width: "220px" }}
            variant="persistent"
            anchor="left"
            open={true}
            classes={{ paper: classes.drawerPaper }}
          >
            ...
          </Drawer>

        </Route>
        <Switch>
          <Route exact path="/">
            <Container>
              ...
            </Container>
          </Route>
          <Route exact path="/about">
            <Container>
              ...
            </Container>
          </Route>
        </Switch>
      </div>
    </Router>
  );
}

演示

在iframe维度或响应视图中四处游玩

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66085877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档