gpt4 book ai didi

javascript - 使用 Material UI 响应路由器

转载 作者:行者123 更新时间:2023-12-02 20:56:08 26 4
gpt4 key购买 nike

我对 React 还很陌生,现在正在尝试一起使用 React Router 和 Material UI。

我的布局文件使用 Material UI 中的抽屉导航,我将菜单放在左侧,内容放在右侧,如下图所示

我想做的是,当我点击Test Comp时它应该加载 TestComp.js右侧的组件,依此类推 Test Comp One加载TestCompOne.js .

我已经浏览了带有链接、开关和路由的 ReactRouter 代码,但我不确定将它们放在哪里。

我尝试为我的布局文件添加以下代码,该文件应该有路由器,但我确信我做得不对我得到的错误是

Error: Invariant failed: You should not use <Link> outside a <Router>

请您纠正我,让我明白这一点

enter image description here

index.js

import Layout from './Layout/Layout'

ReactDOM.render(
<React.StrictMode>
<Layout/>
</React.StrictMode>,
document.getElementById('root')
);

组件 - TestComp.js

import React from "react"

export default class TestComp extends React.Component {
render() {
return (
<h1>test component for test</h1>
);
}
}

组件TestCompOne.js

import React from "react"

export default class TestCompOne extends React.Component {
render() {
return (
<h1>test component ONE for test</h1>
);
}
}

我正在粘贴整个布局文件,该文件有点大,但主要区域是 drawer这是左侧菜单和要显示的内容,我在其中添加了 <Router> 。抱歉,如果这是完全错误的做法,请提出建议

Component Layout.js - 它具有来自 Material UI 的抽屉导航,我想在其中添加路由器以打开各个组件

import React from 'react';
import PropTypes from 'prop-types';
import AppBar from '@material-ui/core/AppBar';
import CssBaseline from '@material-ui/core/CssBaseline';
import Divider from '@material-ui/core/Divider';
import Drawer from '@material-ui/core/Drawer';
import Hidden from '@material-ui/core/Hidden';
import IconButton from '@material-ui/core/IconButton';
import InboxIcon from '@material-ui/icons/MoveToInbox';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import MailIcon from '@material-ui/icons/Mail';
import MenuIcon from '@material-ui/icons/Menu';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import { makeStyles, useTheme } from '@material-ui/core/styles';
import { MenuItem, MenuList } from '@material-ui/core';
import TestComp from '../Components/TestComp'
import TestCompOne from '../Components/TestCompOne'
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";

const drawerWidth = 240;

const useStyles = makeStyles((theme) => ({
root: {
display: 'flex',
},
drawer: {
[theme.breakpoints.up('sm')]: {
width: drawerWidth,
flexShrink: 0,
},
},
appBar: {
[theme.breakpoints.up('sm')]: {
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: drawerWidth,
},
},
menuButton: {
marginRight: theme.spacing(2),
[theme.breakpoints.up('sm')]: {
display: 'none',
},
},
// necessary for content to be below app bar
toolbar: theme.mixins.toolbar,
drawerPaper: {
width: drawerWidth,
},
content: {
flexGrow: 1,
padding: theme.spacing(3),
},
}));

function ResponsiveDrawer(props) {
const { container } = props;
const classes = useStyles();
const theme = useTheme();
const [mobileOpen, setMobileOpen] = React.useState(false);
const [sampleText, setSampleText] = React.useState("test");

const handleDrawerToggle = () => {
setMobileOpen(!mobileOpen);
};

const testCompClicked = () => {
setSampleText("Test Comp")
};

const testCompOneClicked = () => {
setSampleText("Test Comp One")
};

const drawer = (
<div>
<div className={classes.toolbar} />
<Divider />
<MenuList>
<MenuItem onClick={testCompClicked} component={Link} to="/testcomp">
<ListItemIcon>
<InboxIcon />
</ListItemIcon>
<ListItemText primary="Test Comp" />
</MenuItem>
<MenuItem onClick={testCompOneClicked} component={Link} to="/testcompone">
<ListItemIcon>
<MailIcon />
</ListItemIcon>
<ListItemText primary="Test Comp One" />
</MenuItem>
</MenuList>
<Divider />
</div>
);

return (
<div className={classes.root}>
<CssBaseline />
<AppBar position="fixed" className={classes.appBar} style={{ background: '#78a02e'}}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
edge="start"
onClick={handleDrawerToggle}
className={classes.menuButton}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap>
Responsive drawer
</Typography>
</Toolbar>
</AppBar>
<nav className={classes.drawer} aria-label="mailbox folders">
{/* The implementation can be swapped with js to avoid SEO duplication of links. */}
<Hidden smUp implementation="css">
<Drawer
container={container}
variant="temporary"
anchor={theme.direction === 'rtl' ? 'right' : 'left'}
open={mobileOpen}
onClose={handleDrawerToggle}
classes={{
paper: classes.drawerPaper,
}}
ModalProps={{
keepMounted: true, // Better open performance on mobile.
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden xsDown implementation="css">
<Drawer
classes={{
paper: classes.drawerPaper,
}}
variant="permanent"
open
>
{drawer}
</Drawer>
</Hidden>
</nav>
<main className={classes.content}>
<div className={classes.toolbar} />
<h1>{sampleText}</h1>
<h3>I want to show individual components using React Rounter when I press them from the left menu</h3>
<Router>
<Switch>
<Route path="/testcomp">
<TestComp />
</Route>
<Route path="/testcompone">
<TestCompOne />
</Route>
<Route path="/">
<TestComp />
</Route>
</Switch>
</Router>
</main>
</div>
);
}

ResponsiveDrawer.propTypes = {
/**
* Injected by the documentation to work in an iframe.
* You won't need it on your project.
*/
container: PropTypes.any,
};

export default ResponsiveDrawer;

最佳答案

Links需要在 <Router> 范围内环境。尝试一下

  return (
<Router>
<div className={classes.root}>
<CssBaseline />
<AppBar position="fixed" className={classes.appBar} style={{ background: '#78a02e'}}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
edge="start"
onClick={handleDrawerToggle}
className={classes.menuButton}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap>
Responsive drawer
</Typography>
</Toolbar>
</AppBar>
<nav className={classes.drawer} aria-label="mailbox folders">
{/* The implementation can be swapped with js to avoid SEO duplication of links. */}
<Hidden smUp implementation="css">
<Drawer
container={container}
variant="temporary"
anchor={theme.direction === 'rtl' ? 'right' : 'left'}
open={mobileOpen}
onClose={handleDrawerToggle}
classes={{
paper: classes.drawerPaper,
}}
ModalProps={{
keepMounted: true, // Better open performance on mobile.
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden xsDown implementation="css">
<Drawer
classes={{
paper: classes.drawerPaper,
}}
variant="permanent"
open
>
{drawer}
</Drawer>
</Hidden>
</nav>
<main className={classes.content}>
<div className={classes.toolbar} />
<h1>{sampleText}</h1>
<h3>I want to show individual components using React Rounter when I press them from the left menu</h3>
<Switch>
<Route path="/testcomp">
<TestComp />
</Route>
<Route path="/testcompone">
<TestCompOne />
</Route>
<Route path="/">
<TestComp />
</Route>
</Switch>
</main>
</div>
</Router>
);

关于javascript - 使用 Material UI 响应路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61479525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com