我是新来的反应者。我正在学习如何在React中以新路径打开新页面。
这是我的index.js
import React from "react";
import ReactDOM from "react-dom";
import { createBrowserHistory } from "history";
import { Router, Route, Switch } from "react-router-dom";
import "assets/css/material-dashboard-react.css?v=1.5.0";
import indexRoutes from "routes/index.jsx";
import CreateUser from "views/User/CreateUser.jsx";
const hist = createBrowserHistory();
ReactDOM.render(
<Router history={hist}>
<Switch>
{indexRoutes.map((prop, key) => {
return <Route path={prop.path} component={prop.component} key={key}
/>;
})}
</Switch>
</Router>,
document.getElementById("root")
);
indexRoutes将呈现我的仪表板
import Dashboard from "@material-ui/icons/Dashboard";
import Person from "@material-ui/icons/Person";
import LibraryBooks from "@material-ui/icons/LibraryBooks";
import BubbleChart from "@material-ui/icons/BubbleChart";
import LocationOn from "@material-ui/icons/LocationOn";
import Notifications from "@material-ui/icons/Notifications";
import Unarchive from "@material-ui/icons/Unarchive";
import DashboardPage from "views/Dashboard/Dashboard.jsx";
import UserProfile from "views/UserProfile/UserProfile.jsx";
import TableList from "views/TableList/TableList.jsx";
import Typography from "views/Typography/Typography.jsx";
import Icons from "views/Icons/Icons.jsx";
import Maps from "views/Maps/Maps.jsx";
import NotificationsPage from "views/Notifications/Notifications.jsx";
import UpgradeToPro from "views/UpgradeToPro/UpgradeToPro.jsx";
import User from "views/User/User.jsx";
const dashboardRoutes = [
{
path: "/dashboard",
sidebarName: "Dashboard",
navbarName: "Material Dashboard",
icon: Dashboard,
component: DashboardPage
},
{
path: "/profile",
sidebarName: "User Profile",
navbarName: "Profile",
icon: Person,
component: UserProfile
},
{
path: "/table",
sidebarName: "Table List",
navbarName: "Table List",
icon: "content_paste",
component: TableList
},
{
path: "/typography",
sidebarName: "Typography",
navbarName: "Typography",
icon: LibraryBooks,
component: Typography
},
{
path: "/icons",
sidebarName: "Icons",
navbarName: "Icons",
icon: BubbleChart,
component: Icons
},
{
path: "/maps",
sidebarName: "Maps",
navbarName: "Map",
icon: LocationOn,
component: Maps
},
{
path: "/notifications",
sidebarName: "Notifications",
navbarName: "Notifications",
icon: Notifications,
component: NotificationsPage
},
{
path: "/user",
sidebarName: "User",
navbarName: "User",
icon: Person,
component: User
},
{
path: "/upgrade-to-pro",
sidebarName: "Upgrade To PRO",
navbarName: "Upgrade To PRO",
icon: Unarchive,
component: UpgradeToPro
},
{ redirect: true, path: "/", to: "/dashboard", navbarName: "Redirect" }
];
export default dashboardRoutes;
但是,我想通过单击一个按钮来打开一个新页面。例如,这是我的仪表板中的用户页面。我通过单击侧边栏中路径为localhost / user的用户成功访问了此用户页面。我在用户页面上创建了一个按钮。单击按钮时,我想导航从localhost / user到localhost / createuser的路径并显示createuser页面。
import React from "react";
import withStyles from "@material-ui/core/styles/withStyles";
import GridItem from "components/Grid/GridItem.jsx";
import GridContainer from "components/Grid/GridContainer.jsx";
import Table from "components/Table/Table.jsx";
import Card from "components/Card/Card.jsx";
import CardHeader from "components/Card/CardHeader.jsx";
import CardBody from "components/Card/CardBody.jsx";
import Button from "components/CustomButtons/Button.jsx";
import CardFooter from "components/Card/CardFooter.jsx";
const styles = {
cardCategoryWhite: {
"&,& a,& a:hover,& a:focus": {
color: "rgba(255,255,255,.62)",
margin: "0",
fontSize: "14px",
marginTop: "0",
marginBottom: "0"
},
"& a,& a:hover,& a:focus": {
color: "#FFFFFF"
}
},
cardTitleWhite: {
color: "#FFFFFF",
marginTop: "0px",
minHeight: "auto",
fontWeight: "300",
fontFamily: "'Roboto', 'Helvetica', 'Arial', sans-serif",
marginBottom: "3px",
textDecoration: "none",
"& small": {
color: "#777",
fontSize: "65%",
fontWeight: "400",
lineHeight: "1"
}
}
};
function createUser() {
console.log("lolo");
}
function TableList(props) {
const { classes } = props;
return (
<GridContainer>
<GridItem xs={12} sm={12} md={12}>
<Button color="primary" onClick={createUser()}>Create User</Button>
<Card>
<CardHeader color="primary">
<h4 className={classes.cardTitleWhite}>User List</h4>
<p className={classes.cardCategoryWhite}>
{/*Here is a subtitle for this table*/}
</p>
</CardHeader>
<CardBody>
<Table
tableHeaderColor="primary"
tableHead={["Name", "Country", "City", "Salary"]}
tableData={[
["Dakota Rice", "Niger", "Oud-Turnhout", "$36,738"],
["Minerva Hooper", "Curaçao", "Sinaai-Waas", "$23,789"],
["Sage Rodriguez", "Netherlands", "Baileux", "$56,142"],
["Philip Chaney", "Korea, South", "Overland Park", "$38,735"],
["Doris Greene", "Malawi", "Feldkirchen in Kärnten", "$63,542"],
["Mason Porter", "Chile", "Gloucester", "$78,615"],
["LALA", "NOOB", "LOL", "$12345"]
]}
/>
</CardBody>
<CardFooter>
<Button color="primary">Update Profile</Button>
</CardFooter>
</Card>
</GridItem>
</GridContainer>
);
}
export default withStyles(styles)(TableList);
我尝试了此link中显示的几种方法,但是它们都不起作用。有人对此有任何想法吗?