React-Router:组件未呈现但 URL 更改

React-Router: Component is not rendered but URL changes

我是 React js 新手。 我正在实现抽屉 (material-ui),因此当用户单击左侧菜单时 (LeftMenu.jsx);相应的组件应该在中心呈现(即 MainContent.jsx)

不幸的是,我收到以下错误: Error: Invariant failed: You should not use <Link> outside a <Router> 但是,如果我将 Link 包含在 LeftMenu.jsx 中,那么 URL 会发生变化,但视图不会得到渲染。

我尝试了React router changes url but not view中给出的解决方案,但问题仍然没有解决。

以下是组件列表:

Status.jsx

import React, { Component } from "react";
class Status extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  render() {
    return <div>Status</div>;
  }
}
export default Status;

AdminPanelLayout.jsx

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

  const handleDrawerOpen = () => {
    if (open) {
      setOpen(false);
    } else {
      setOpen(true);
    }
  };

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

  return (
    <div className={classes.root}>
      <CssBaseline />
      <AppBar
        position="fixed"
        className={clsx(classes.appBar, {
          [classes.appBarShift]: open,
        })}
      >
        <Toolbar>
          <IconButton
            color="inherit"
            aria-label="open drawer"
            onClick={handleDrawerOpen}
            edge="start"
          >
            <MenuIcon />
          </IconButton>
          <Typography variant="h6" noWrap>
            Admin Panel
          </Typography>
        </Toolbar>
      </AppBar>
      <Drawer
        variant="permanent"
        className={clsx(classes.drawer, {
          [classes.drawerOpen]: open,
          [classes.drawerClose]: !open,
        })}
        classes={{
          paper: clsx({
            [classes.drawerOpen]: open,
            [classes.drawerClose]: !open,
          }),
        }}
      >
        <div className={classes.toolbar}></div>
        <Divider />
        <LeftMenu></LeftMenu>
      </Drawer>
      <MainContent></MainContent>
    </div>
  );
}

MainContent.jsx

export default function MainContent() {
  const classes = useStyles();
  return (
    <Router>
      <main className={classes.content}>
        <div className={classes.toolbar} />
        <Switch>
          <Route path="/Status" component={withRouter(Status)}>
            <Status />
          </Route>
          <Route path="/">
            <span>testing</span>
          </Route>
        </Switch>
      </main>
    </Router>
  );
}

LeftMenu.jsx

export default function LeftMenu() {
  return (
    <div>
      <List>
        {["Status", "Starred", "Send email", "Drafts"].map((text, index) => (
          <ListItem button key={text} component={Link} to="/Status">
            <ListItemIcon>
              {index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
            </ListItemIcon>
            <ListItemText primary={text} />
          </ListItem>
        ))}
      </List>
      <Divider />
      <List>
        {["All mail", "Trash", "Spam"].map((text, index) => (
          <ListItem button key={text}>
            <ListItemIcon>
              {index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
            </ListItemIcon>
            <ListItemText primary={text} />
          </ListItem>
        ))}
      </List>
    </div>
  );
}

App.js

import "./App.css";
import AdminPanelLayout from "./components/AdminPanelLayout";
import LeftMenu from "./components/LeftMenu";

function App() {
  return <AdminPanelLayout></AdminPanelLayout>;
}

export default App;

您应该使用

<Route path="/Status" component={Status}>

<Route path="/Status">
 <Status />
</Route>

另外你不应该这样做 component={withRouter(Status)} 因为这等同于 component={Status}withRouter 的目的是让未通过 Route 呈现的组件访问 route props 。由于 Status 已经通过 Route 渲染,因此不需要用 withRouter 包装它。

codesanbox - 希望对您有所帮助!