如何创建react-router v4面包屑?我尝试通过问题单在react-router V4网站上提出这个问题。他们只是说要看recursive paths的例子。我真的想在semantic-ui-react
中创建它答案 0 :(得分:17)
我是在追求同样的事情,你的问题指出了我正确的方向。
这对我有用:
const Breadcrumbs = (props) => (
<div className="breadcrumbs">
<ul className='container'>
<Route path='/:path' component={BreadcrumbsItem} />
</ul>
</div>
)
const BreadcrumbsItem = ({ match, ...rest }) => (
<span>
<li className={match.isExact ? 'breadcrumb-active' : undefined}>
<Link to={match.url || ''}>
{match.url}
</Link>
</li>
<Route path={`${match.url}/:path`} component={BreadcrumbsItem} />
</span>
)
答案 1 :(得分:4)
我将semantic-ui-react
用于我自己的项目,并根据location.pathname
创建了面包屑;
export default (props) => {
const paths = props.pathname.split('/').map((p, i, arr) => {
if (i === 0) return {
key: i,
content: (<Link to={'/'}>home</Link>),
active: (i === arr.length - 1),
link: (i < arr.length - 1)
};
if (i === arr.length - 1) return {
key: i,
content: p,
active: (i === arr.length - 1)
};
return {
key: i,
content: (<Link to={`${arr.slice(0, i + 1).join('/')}`}>{p}</Link>),
active: (i === arr.length - 1),
link: (i < arr.length - 1)}
};
);
return <Breadcrumb icon='chevron right' sections={paths}/>;
};
答案 2 :(得分:4)
这两种方法的问题在于您只能在痕迹路径中使用路径名;也就是说,您必须将路由绑定到路径的演示文稿名称。
答案 3 :(得分:4)
这可以使用HOC来完成,该HOC从react-router解析pathname
并返回匹配。虽然有点冗长,但我认为它提供了更大的灵活性和良好的可读性痕迹配置对象数组。
Breadcrumbs.jsx
import React from 'react';
import { NavLink } from 'react-router-dom';
import { withBreadcrumbs } from 'withBreadcrumbs';
const UserBreadcrumb = ({ match }) =>
<span>{match.params.userId}</span>; // use match param userId to fetch/display user name
const routes = [
{ path: 'users', breadcrumb: 'Users' },
{ path: 'users/:userId', breadcrumb: UserBreadcrumb},
{ path: 'something-else', breadcrumb: ':)' },
];
const Breadcrumbs = ({ breadcrumbs }) => (
<div>
{breadcrumbs.map(({ breadcrumb, path, match }) => (
<span key={path}>
<NavLink to={match.url}> // wrap breadcrumb with semantic-ui element
{breadcrumb}
</NavLink>
<span>/</span>
</span>
))}
</div>
);
export default withBreadcrumbs(routes)(Breadcrumbs);
withBreadcrumbs.js
import React from 'react';
import { matchPath, withRouter } from 'react-router';
const renderer = ({ breadcrumb, match }) => {
if (typeof breadcrumb === 'function') { return breadcrumb({ match }); }
return breadcrumb;
};
export const getBreadcrumbs = ({ routes, pathname }) => {
const matches = [];
pathname
.replace(/\/$/, '')
.split('/')
.reduce((previous, current) => {
const pathSection = `${previous}/${current}`;
let breadcrumbMatch;
routes.some(({ breadcrumb, path }) => {
const match = matchPath(pathSection, { exact: true, path });
if (match) {
breadcrumbMatch = {
breadcrumb: renderer({ breadcrumb, match }),
path,
match,
};
return true;
}
return false;
});
if (breadcrumbMatch) {
matches.push(breadcrumbMatch);
}
return pathSection;
});
return matches;
};
export const withBreadcrumbs = routes => Component => withRouter(props => (
<Component
{...props}
breadcrumbs={
getBreadcrumbs({
pathname: props.location.pathname,
routes,
})
}
/>
));
这里也提供开源HOC: https://github.com/icd2k3/react-router-breadcrumbs-hoc
答案 4 :(得分:2)
来自react-router
doc:<Route>
组件在位置与路径的路径匹配时呈现一些组件:
<Route path={`${match.url}/:topicId`} component={Topic}/>
对于信息的基本责任可用于渲染的组件,在这种情况下为<Topic>
。它知道如何获取数据,或者它已经绑定了redux状态等等。所以<Topic>
简单地实例化breadcrums项目代理并将所需数据传递给它:
import {BreadcrumbsItem} from 'react-breadcrumbs-dynamic'
const Topic = ({ match, topic }) => (
<div>
<h3>
{topic.title}
</h3>
<BreadcrumbsItem to={`${match.url}/${match.params.topicId}`}>
{topic.title}
</BreadcrumbsItem>
...
</div>
)
这就是全部。 this answer中的更完整示例。这里住demo。
答案 5 :(得分:1)
尝试这个简单的解决方案:
const Breadcrumbs = ({ ...rest, match }) => (
<span>
<Link to={match.url || ''} className={match.isExact ? 'breadcrumb active' : 'breadcrumb'}>
{match.url.substr(match.url.lastIndexOf('/')+1, match.url.length)}
</Link>
<Route path={`${match.url}/:path`} component={Breadcrumbs} />
</span>
)
你的css:
.breadcrumbs {
background: #fff;
margin-bottom: 15px;
}
.breadcrumb {
margin-bottom: 0;
line-height: 2.5;
display: inline-block;
}
.breadcrumb::before {
display: inline-block;
padding-right: 5px;
padding-left: 5px;
color: #818a91;
content: "/"; }
.breadcrumb.active {
color: #818a91; }
然后像这样使用它:
<div className="container-fluid breadcrumbs">
<Route path='/:path' component={Breadcrumbs} />
</div>
快乐的编码!
答案 6 :(得分:1)
这是为嵌套导航和面包屑提供单一事实来源的解决方案。
该示例应用程序可在GitHub上使用:https://github.com/sneas/react-nested-routes-example
演示:https://sneas.github.io/react-nested-routes-example/
导航配置:
select
d.dept_name,
sum(case when e.position = 'tech_support' then 1 else 0 end) tech_support,
sum(case when e.position = 'data_entry' then 1 else 0 end) data_entry,
sum(case when e.position = 'assistant_manager' then 1 else 0 end) assistant_manager
from department d left join employee e
on e.deptid = d.department_id
group by d.department_id, d.dept_name
我们必须递归地展平导航并将其呈现为平面数组:
export const navigation = [
{
path: "/",
label: "All categories",
content: () => <AllCategories />,
routes: [
{
path: "/electronics",
label: "Electronics",
content: () => <Electronics />,
routes: [
{
path: "/accessories",
label: "Accessories",
content: () => <Accessories />,
routes: [
{
path: "/usb-cables",
label: "USB cables",
content: () => <UsbCables />
}
]
},
{
path: "/headphones",
label: "Headphones",
content: () => <Headphones />
}
]
}
]
}
];
然后使用相同的导航结构构建面包屑。