【发布时间】:2021-03-05 16:38:51
【问题描述】:
我有一个问题,我在侧边栏中使用了 material-ui,但路径不起作用。这是我的代码:
import React, { useState } from "react";
import { NavLink as RouterLink } from "react-router-dom";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
export const Sidebar = (props) => {
const { menu } = useSelector((state) => state);
const [selectedIndex, setSelectedIndex] = useState(1);
const handleListItemClick = (event, index) => {
setSelectedIndex(index);
};
return (
<List component="nav">
{menu.map((text, index) => (
<ListItem
key={text.id}
selected={selectedIndex === text.id}
onClick={(event) => handleListItemClick(event, text.id)}
component={RouterLink}
to={text.url}
button
>
.....
</ListItem>
))}
</List>
);
};
点击菜单会重新加载整个页面。
SessionRouter - This is the component where I call the Sidebar
AppRouter - This is where i call SessionRouter
请帮忙。
【问题讨论】:
-
所以您正在构建的菜单正在正常工作,不是吗?问题是你的屏幕没有变化?
-
它不会改变组件,域也不会,例如,一开始这个 domain.com/ruta1 我希望它去 domain.com/ruta2 但它总是停留在域。 com/ruta1,尝试放置包裹 Listitem 的 Navlink,如果它改变它但重新加载整个页面
-
在您的 to= {text.url} 中,您确实在路线之前通过了 / 吗?
-
是的,数组的一个例子是 = [{label: "Iniciar una queja", id: 1, url:"/iniciarqueja"}]
-
你能试试这个吗?并将重定向放在所有路由的顶部
标签: reactjs material-ui react-router-dom