ant-design/components/breadcrumb/demo/react-router.md

103 lines
2.2 KiB
Markdown
Raw Normal View History

---
order: 3
iframe: 200
reactRouter: react-router-dom
title:
zh-CN: react-router V6
en-US: react-router V6
---
## zh-CN
`react-router@6+` 结合使用,生成和路由绑定的面包屑。
## en-US
Used together with `react-router@6+`.
2019-05-07 14:57:32 +08:00
```jsx
import { HashRouter, Route, Routes, Link, useLocation } from 'react-router-dom';
import { Breadcrumb, Alert } from 'antd';
const Apps = () => (
<ul className="app-list">
<li>
<Link to="/apps/1">Application1</Link><Link to="/apps/1/detail">Detail</Link>
</li>
<li>
<Link to="/apps/2">Application2</Link><Link to="/apps/2/detail">Detail</Link>
</li>
</ul>
);
const breadcrumbNameMap = {
'/apps': 'Application List',
'/apps/1': 'Application1',
'/apps/2': 'Application2',
'/apps/1/detail': 'Detail',
'/apps/2/detail': 'Detail',
};
const Home = props => {
const location = useLocation();
const pathSnippets = location.pathname.split('/').filter(i => i);
const extraBreadcrumbItems = pathSnippets.map((_, index) => {
const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
return (
<Breadcrumb.Item key={url}>
2019-05-07 14:57:32 +08:00
<Link to={url}>{breadcrumbNameMap[url]}</Link>
</Breadcrumb.Item>
);
});
2019-05-07 14:57:32 +08:00
const breadcrumbItems = [
<Breadcrumb.Item key="home">
<Link to="/">Home</Link>
2019-05-07 14:57:32 +08:00
</Breadcrumb.Item>,
].concat(extraBreadcrumbItems);
return (
<div className="demo">
<div className="demo-nav">
<Link to="/">Home</Link>
<Link to="/apps">Application List</Link>
</div>
<Routes>
<Route path="/apps" element={<Apps />} />
<Route path="*" element={<span>Home Page</span>} />
</Routes>
<Alert style={{ margin: '16px 0' }} message="Click the navigation above to switch:" />
2019-05-07 14:57:32 +08:00
<Breadcrumb>{breadcrumbItems}</Breadcrumb>
</div>
);
};
export default () => (
<HashRouter>
<Home />
</HashRouter>
2018-11-28 15:00:03 +08:00
);
2019-05-07 14:57:32 +08:00
```
2019-05-07 14:57:32 +08:00
```css
.demo {
margin: 16px;
}
.demo-nav {
height: 30px;
margin-bottom: 16px;
line-height: 30px;
background: #f8f8f8;
}
.demo-nav a {
padding: 0 8px;
line-height: 30px;
}
.app-list {
margin-top: 16px;
}
2019-05-07 14:57:32 +08:00
```
<style>
[data-theme="dark"] .demo-nav {
background: #141414;
}
</style>