reactjs - 如何链接到 MUI 迷你抽屉侧边栏中的另一个页面?

标签 reactjs react-router material-ui sidebar

我尝试用 封装我的 ListItem 组件,但它不起作用,我也尝试使用历史选项,但这让我感到困惑。 我的代码沙箱:
codesandbox.io/s/funny-einstein-deuqhi?file=/src/App.js

这是我的 sidebar.js(没有 mixin&drawerheader 等的所有导入和创建,应用程序栏上到我的 createsite 的链接有效,我希望我的侧边栏列表项也有相同的效果。我希望我的列表项带有文本 。”添加待办事项”以链接到“\create”页面):

const openedMixin = (theme) => (...);
const closedMixin = (theme) => (...);
const DrawerHeader = styled(...);
const AppBar = styled(...);
const Drawer = styled(...);

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

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

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

  const itemsList = [
    {
      text: "Calendar",
      icon: <EventAvailableOutlinedIcon style={{ fill: 'white' }} />,
    },
    {
      text: "Add To-do",
      icon: <AddBoxTwoToneIcon style={{ fill: 'white' }} />
    }
  ]

  return (
    <Router>
      <Box sx={{ display: 'flex' }}>
        <CssBaseline />
        <AppBar position="fixed" open={open} style={{ background: 'white' }}>
          <Toolbar>
            <IconButton
              color="inherit"
              aria-label="open drawer"
              onClick={handleDrawerOpen}
              edge="start"
              sx={{
                marginRight: 5,
                ...(open && { display: 'none' }),
              }}
            >
              <MenuIcon style={{ fill: '#85CEE9' }} />
            </IconButton>
            <Link to="/create">create</Link>
            <Typography variant="h6" noWrap component="div" style={{ color: "#85CEE9" }}>
              project
            </Typography>
          </Toolbar>
        </AppBar>
        <Drawer variant="permanent" open={open}>
          <DrawerHeader>
            <IconButton onClick={handleDrawerClose}>
              {theme.direction === 'rtl' ? <ChevronRightIcon style={{ fill: 'white' }} /> : <ChevronLeftIcon style={{ fill: 'white' }} />}
            </IconButton>
          </DrawerHeader>
          <Divider />
          <List>
            {itemsList.map((item, index) => {
              const { text, icon } = item;
              return (
                // <Link to="/create">
                <Link to="/create">
                  <ListItem button component={Link} to="/create" onClick={onItemClick('Page 2')} key={text}>
                    {icon && <ListItemIcon >{icon}</ListItemIcon>}
            // <ListItemText primary={text} style={{ color: "white" }} />
                    <Link to="/create">create</Link>
                  </ListItem>
                </Link>
                // </Link>
              );
            })}
          </List>
        </Drawer>
      </Box>
    </Router>
  );
}```

最佳答案

在您链接的沙箱中,您根本没有使用抽屉中的 Link 组件,因此没有链接到任何内容。

ListItem 组件渲染为 Link 组件并传递适当的链接属性。

示例:

const itemsList = [
  {
    text: "Calendar",
    icon: <EventAvailableOutlinedIcon style={{ fill: "white" }} />,
    to: "/create" // <-- add link targets
  },
  {
    text: "Add To-do",
    icon: <AddBoxTwoToneIcon style={{ fill: "white" }} />,
    to: "/add-todo"
  }
];

对于 ListItem 组件,指定 Link 组件作为渲染列表项的组件,并传递当前映射的 itemto 属性。

<List>
  {itemsList.map((item, index) => {
    const { text, icon } = item;
    return (
      <ListItem component={Link} to={item.to} button key={text}>
        {icon && <ListItemIcon>{icon}</ListItemIcon>}
        <ListItemText primary={text} style={{ color: "white" }} />
      </ListItem>
    );
  })}
</List>

Edit how-do-i-link-to-another-page-in-my-mui-mini-drawer-sidebar

关于reactjs - 如何链接到 MUI 迷你抽屉侧边栏中的另一个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72239170/

相关文章:

javascript - 使用循环避免 React "no-unused-vars"错误

javascript - ./src/index.js第10行: 'elementid' is not defined no-undef

javascript - 单击全选按钮后检查/取消选中复选框的 reducer 功能 -redux React

javascript - ReactJS + react 路由器 : How to disable link from React-Router's <Link>?

javascript - React 路由器问题 : React render two components in the same page

javascript - 如何让Material-UI DropDownMenu返回选定的

javascript - CodeSandbox 出现 Material-UI 错误 : undefined is not an object (evaluating '_context$muiTheme.borderRadius' )

reactjs - 无法通过滑动手势拉出 Material-ui 的 Drawer 组件

javascript - 如何获取带有对象的深度嵌套数组中的最后一个子元素

node.js - 警告 : React attempted to reuse markup in a container but the checksum was invalid