简体   繁体   English

如何在 reactjs 中使用 material ui 在页面上导航

[英]how to navigate on pages using material ui in reactjs

I am making a drawer for my dashboard using material-Ui, I have faced an issue navigating on my pages我正在使用 material-Ui 为我的仪表板制作一个抽屉,我在浏览我的页面时遇到了问题

first, I wanna share my app.首先,我想分享我的应用程序。 js and here I have product route js,这里我有产品路线

<Provider store={store}>
<BrowserRouter>
    < Routes>
      <Route exact path='/' element={<Login/>} />
      <Route exact path='/dashboard' element={<Dashboard/>} />
      <Route exact path='/product' element={<Product/>}/>
    </Routes>
  </BrowserRouter>
</Provider>

here is my dashboard code:这是我的仪表板代码:

In this code, I am an object who has an onClick function for navigation on pages在这段代码中,我是一个 object,他有一个onClick function 用于页面导航

  const listItem = [
    {
      text:'Dashboard',
      onclick:()=>navigate("/dashboard")
    }, 
    {
      text:'Prdocut',
      onclick:()=>navigate("/product")
    } 
  ]

here is my list of who is rendering the items这是我的渲染项目列表

<List>
  {listItem.map((item, index) => {
    const { text,onClick} = item;
      return (
         <ListItem button key={text} onClick={onClick}>
            <ListItemText primary={text} />
         </ListItem>
        )})}
</List>

but it's not navigation my my product page但它不是导航我的产品页面

Where is the navigate function definition? navigate function 定义在哪里? This should be on the body of the component, using the useNavigate hook from React Router DOM... So your code should look like这应该在组件的主体上,使用来自 React Router DOM 的useNavigate钩子......所以你的代码应该看起来像

// on top of the file:
import { useNavigate } from 'react-router-dom';

// on your component:
const navigate = useNavigate();

const listItem = [
    {
      text:'Dashboard',
      onclick:()=>navigate("/dashboard")
    }, 
    {
      text:'Prdocut',
      onclick:()=>navigate("/product")
    } 
];

return (
    <List>
      {listItem.map((item, index) => {
        const { text, onclick } = item;
          return (
             <ListItem button key={text} onClick={onclick}>
                <ListItemText primary={text} />
             </ListItem>
            )})}
    </List>
);

As a more simple alternative, you may like to use Link component of React Router DOM, importing it like import { Link } from 'react-router-dom';作为更简单的替代方案,您可能喜欢使用 React Router DOM 的Link组件,像import { Link } from 'react-router-dom'; and using it like this on the jsx:并在 jsx 上像这样使用它:

const listItem = [
    {
      text:'Dashboard',
      onclick: '/product'
    }, 
    {
      text:'Product',
      onclick: '/product',
    } 
];

return (
    <List>
      {listItem.map((item, index) => {
        const { text, onclick } = item;
          return (
            <Link key={text} to={onclick}>
                <ListItem button>
                <ListItemText primary={text} />
                </ListItem>
            </Link>
            )})}
    </List>
);

Anyways, the error is on the JSX (specifically, on the map function over listItem).无论如何,错误出在 JSX 上(具体来说,在 listItem 上的 map function 上)。 In your code, you're destructuring an item like { text, onClick } but it should be NOT camelcase onClick, like this: { text, onclick } .在您的代码中,您正在解构像{ text, onClick }这样的项目,但它不应该是驼峰式 onClick,如下所示: { text, onclick }

the error is in onclick but at map function you destructing it as onClick but it is onclick错误在 onclick 但在 map function 你将其破坏为 onClick 但它是 onclick

it should like它应该喜欢

import { useNavigate } from "react-router-dom";

const Home = () => {
  const navigate = useNavigate();
  const listItem = [
    {
      text: "Dashboard",
      onc: () => navigate("/")
    },
    {
      text: "Prdocut",
      onc: () => navigate("/home")
    }
  ];
  return (
    <>
      <ul>
        {listItem.map((item) => {
          const { text, onc } = item;
          return <li onClick={onc}>{text}</li>;
        })}
      </ul>
    </>
  );
};
export default Home;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM