简体   繁体   English

使用带有 reactjs 和 react-router-v5 的面包屑返回到以前的页面时出错

[英]error while go back to previous pages using breadcrumbs with reactjs and react-router-v5

I'm make a breadcrumb using reactjs and react-router v5.我正在使用 reactjs 和 react-router v5 制作面包屑。 I'm getting error when i'm click to previous page in breadcumbs.当我点击面包屑中的上一页时出现错误。 the application becomes hang.应用程序挂起。 here's my breadcrumb.这是我的面包屑。

在此处输入图像描述

showing the recent url pages is work.显示最近的 url 页面是可行的。 But the problem is when i want to back to previous page from breadcrumbs, my apps become hang, and i'm getting this error.但问题是当我想从面包屑返回到上一页时,我的应用程序挂起,并且出现此错误。

在此处输入图像描述

And then i'm getting the weird things about url.然后我得到了关于 url 的奇怪的事情。 when i'm done in the last page.当我完成最后一页时。 when i'm hover the link in breadcrumbs, it looks same.当我将链接悬停在面包屑中时,它看起来是一样的。

在此处输入图像描述

it's weird, because the url of dashboard is '/'.这很奇怪,因为仪表板的 url 是 '/'。

Here's my breadcrumbs code.这是我的面包屑代码。

import React from "react";
import { Breadcrumb } from "react-bootstrap";
import { Link, withRouter } from "react-router-dom";

const Breadcrumbs = (props) => {
    const {
        history,
        location: {pathname}
    } = props;

    const pathnames = pathname.split("/").filter(x => x);

  return (
    <Breadcrumb aria-label="breadcrumb">
        <Link onClick={() => history.push("/")}> Dashboard</Link>

        {
            pathnames.map((name, index) => {
                const routeTo = `/${pathnames.slice(0, index + 1).join("/")}`;
                const isLast = index === pathnames.length - 1;
                return isLast ? (<>
                    <Breadcrumb.Item key={name}>{name}</Breadcrumb.Item> </>
                ) : (
                    <Link key={name}  onClick={() => history.push(routeTo)}>{name}</Link>
                )
            })
        }
    </Breadcrumb>
  );
};

export default  withRouter(Breadcrumbs);

I want my breadcrumbs work like this.我希望我的面包屑导航像这样工作。 Demo Breadcrumbs演示面包屑

i'm stuck.我卡住了。 any help will be appreciated.任何帮助将不胜感激。 Thank you.谢谢你。

The react-router-dom Link component requires a to prop with a valid string or Path object with pathname property. react-router-dom Link组件需要一个带有有效字符串或具有pathname属性的Path对象to prop。

Instead of代替

<Link onClick={() => history.push("/")}>Dashboard</Link>
...
<Link key={name} onClick={() => history.push(routeTo)}>{name}</Link>

use采用

<Link to="/">Dashboard</Link>
...
<Link key={name} to={routeTo}>{name}</Link>

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

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