使用react-breadcrumbs-dynamic
这是完全独立于路由器的解决方案,你可以将其与任何版本的React Router(2或3或4)或任何其他用于React的路由库一起使用,或者完全不进行路由。您只需要指定面包屑项目及其道具的组件。然而道具和部件应符合规定 分离。BreadcrumbsItem
在组件和路由层次结构中的任何位置,都应在中介组件中指定道具。
基础使用
const Profile = ({user}) => (
<div> <BreadcrumbsItem
to=`/user/${user.login}`
icon='account-box'
>
{user.firstName} {user.lastName}
</BreadcrumbsItem> <h1>
{user.firstName} {user.lastName}
</h1>
</div>
)
安装
npm install --save react-through react-breadcrumbs-dynamic
基础配置
你已经使用声明通信通过 react tree react-through?只需添加<ThroughProvider/>
到您的React组件树的根目录:
import {ThroughProvider} from 'react-through' const theApp = (
<ThroughProvider>
<App />
</ThroughProvider>
) ReactDOM.render(theApp, document.getElementById('root'))
实例配置
在此示例中,react-router
v4 <NavLink>
用作面包屑项目:
import {Breadcrumbs} from 'react-breadcrumbs-dynamic' const Page = (props) => (
return (
<div className="App">
<Header> <Breadcrumbs
separator={<b> / </b>}
item={NavLink}
finalItem={'b'}
finalProps={{
style: {color: 'red'}
}}
/> </Header> {props.children} <Footer>
<Breadcrumbs/>
</Footer>
</div>
)
}
请注意,item
并且finalItem
需要react组件(类)而不是react元素。但是separator
需要反应元素。
默认情况下,项目顺序基于URL长度。您可以根据需要覆盖排序顺序,只需在compare
prop中指定比较函数即可,该比较函数接收包含面包屑项prop的一对对象。例如:<Breadcrumbs compare={(a,b)=>a.weight-b.weight} removeProps={{weight: true}} />
。
如果您使用<a>
基于标签的项目,那么您会发现将prop映射到prop 上renameProps
或 将其duplicateProps
有用,如下所示: 。to
href
<Breadcrumbs renameProps={{to:"href"}} />
将项目添加到面包屑
反应树中的每个路由组件通常都与路由和相应的面包屑相关联。每个组件都可以通过<BreadcrumbsItem>
使用任何道具和子项进行渲染来添加其面包屑项目。
具有面包屑项目的路由组件的示例树:
import {BreadcrumbsItem} from 'react-breadcrumbs-dynamic' const App = (props) => (
<div>
<BreadcrumbsItem to='/'>Main Page</BreadcrumbsItem>
{props.children}
<Route exact path="/user" component={User} />
</div>
) const User = (props) => (
<div>
<BreadcrumbsItem to='/user'>Home</BreadcrumbsItem>
<h2>Home</h2>
{props.children}
<Route exact path="/user/contacts" component={Contacts} />
</div>
) const Contacts = (props) => (
<div>
<BreadcrumbsItem to='/user/contacts'>Contacts</BreadcrumbsItem>
<h2>Contacts</h2>
...
</div>
)
您可以通过任何方向通过反应树声明性地传递带有任何数据,函数,组件等的道具,因为 react-through 允许这样做。
结果
上面代码的结果将表示面包屑,如下所示:
<NavLink to='/'>Main Page</NavLink>
<b> / </b>
<NavLink to='/user'>Home</NavLink>
<b> / </b>
<b to='/user/contacts'>Contacts</b>
组成和功能
面包屑实例在实施Breadcrumbs
部件,这是through container
在以下方面 react-through。
属性 | 类型 | 默认 | 描述 |
---|---|---|---|
separator |
元件 | undefined |
面包屑项目之间的分隔符 |
item |
零件 | a |
面包屑项目的组成部分 |
finalItem |
零件 | item 道具价值 |
最终面包屑项目的组成部分 |
finalProps |
宾语 | {} |
最终道具-将覆盖中指定的 BreadcrumbsItem |
container |
零件 | span |
包装器组件 |
containerProps |
宾语 | {} |
道具container 组件 |
compare |
功能 | (a,b)=> a.to.length-b.to.length | 比较功能,用于分类项目 |
hideIfEmpty |
布尔值 | false |
显示或隐藏面包屑容器(如果存在或不存在) |
renameProps |
宾语 | {} |
将道具传递的道具重命名BreadcrumbsItem 为item |
duplicateProps |
宾语 | {} |
重复的道具物品传递BreadcrumbsItem 到item |
removeProps |
宾语 | {} |
道具不会从道具传递BreadcrumbsItem 到item |
该BreadcrumbsItem
组件是through agent
具有支撑力的关键。to
该BreadcrumbsItem
组件可能有任何道具,也可能有孩子。的每个道具BreadcrumbsItem
将传递到中的item
或finalItem
道具中指定的相应面包屑组件Breadcrumbs
。只有一个道具是强制性的。
属性 | 类型 | 默认 | 描述 |
---|---|---|---|
to |
串 | 需要 | 带URL的必填必填方位键 |
... |
任何 | 任何属性-将映射到对应的面包屑项目 |
withBreadcrumbsItem()
功能
高级用法高级组件功能。它获得一个参数与您的自定义组件的反应和返回这将有相应的组件breadcrumbs
在其道具与方法item()
,并items()
像 throughAgent
从react-through。
this.props.breadcrumbs.item()
和 this.props.breadcrumbs.items()
先进的使用方法来配置您的react组件的面包屑项目。这些方法将通过withBreadcrumbsItem
功能HOC添加到props 。该函数item()
接受一个带有props的react组件,该函数 items()
接受带有子代的react组件,子组件可以包含任意数量的组件以创建相应数量的面包屑项。这些功能的面包屑项目可以是任何反应组件,而只有道具才有意义。在这种情况下,Dummy
和Item
组件由库导出。如果不再需要当前组件来表示任何面包屑项目,则每个函数都接受null以将面包屑项目重置为无。
常数
该through area
库使用的名称在breadcrumbsThroughArea
变量中定义 。
包含方位键的道具名称在中定义 breadcrumbsBearingKey
。
import { breadcrumbsThroughArea } from 'react-breadcrumbs-dynamic'
import { breadcrumbsBearingKey } from 'react-breadcrumbs-dynamic'
使用react-breadcrumbs-dynamic的更多相关文章
- [React] Asynchronously Load webpack Bundles through Code-splitting and React Suspense
One approach to building high performance applications with webpack is to take advantage of code-spl ...
- React笔记
React JS Tutorials for Beginners - 1 - Getting Started https://www.youtube.com/watch?v=-AbaV3nrw6E&a ...
- Extjs5.0中的新特性
We are excited that Ext JS 5 is now available! Ext JS 5 introduces many new and exciting improvement ...
- ANGULAR 2 FOR REACT DEVELOPERS
Now that Angular 2 is in beta, the time has come for us to drop everything and learn something new, ...
- [转] React Native Navigator — Navigating Like A Pro in React Native
There is a lot you can do with the React Native Navigator. Here, I will try to go over a few example ...
- React和动态网站接口的经济学
来自: React and the economics of dynamic web interfaces 自从2000开始我就一直在做web开发,曾见过很多以各种库和框架的起起落落,这些库和框架作为 ...
- React配合Webpack实现代码分割与异步加载
这是Webpack+React系列配置过程记录的第四篇.其他内容请参考: 第一篇:使用webpack.babel.react.antdesign配置单页面应用开发环境 第二篇:使用react-rout ...
- React服务器端渲染值Next.js
昨天leader给分配了新任务,让熟悉一下ssr,刚开始有点懵,啥玩意?百度了一下,不就是服务器端渲染(server side render,简称: ssr). ssr简介 服务端渲染一个很常见的场景 ...
- 记React+.NetCore API实现动态列导出
1.效果演示 2.用到的第三方类库 前端:React,Dva,Antd 后端:ASP.NET CORE,System.Linq.Dynamic.Core,EPPlus.Core 3.基本思路 第一:E ...
- react源码总览(翻译)
用react也有段时间了, 是时候看看人家源码了. 看源码之前看到官方文档 有这么篇文章介绍其代码结构了, 为了看源码能顺利些, 遂决定将其翻译来看看, 小弟英语也是半瓢水, 好多单词得查词典, 不当 ...
随机推荐
- NOIP模拟 5
考试的时候相当浮躁,而且脑子并不工作 T1看了几眼,觉得没思路,先skip T2一打眼,满足条件的最大值,二分!(然后就死了,根本没想有没有单调性) T3找了半天规律,一开始自己手模的K3都过不了样例 ...
- 使用FinalShell 安装jdk和tomcat流程(Linux系统是centOS7.5)
本文是作者原创,版权归作者所有.若要转载,请注明出处 我今天刚刚买了一个一年的百度云服务器,85元,还是很便宜的,正好用来练练linux,至于为什么使用FinalShell 而不是xshell,因为F ...
- Java 博客系统 Tale
Tale Tale的英文含义为故事,我相信每个坚持写Blog的人都是有故事的:中文你叫它 塌了 也无所谓 . Tale 使用了轻量级mvc框架 Blade 开发,默认主题使用了漂亮的 pinghsu, ...
- vue的相关知识
一.DOM vs 函数库 vs框架 DOM: API繁琐 函数库:JQuery对DOM的每个步骤的API进行一对一的简化,但并没有改变DOM做事的步骤和方法. 框架:一个包含部分已经实现的功能的半成 ...
- spark集群搭建(三台虚拟机)——kafka集群搭建(4)
!!!该系列使用三台虚拟机搭建一个完整的spark集群,集群环境如下: virtualBox5.2.Ubuntu14.04.securecrt7.3.6_x64英文版(连接虚拟机) jdk1.7.0. ...
- 使用iis反向代理.net core应用程序
.net core 其实是自宿主性质的web应用程序,而不再是web网站,所以.net core是可以直接单独作为系统服务部署.但是实际情况中,为了同个一个端口能支持多个web应用和统一管理,还是应该 ...
- 附009.Kubernetes永久存储之GlusterFS独立部署
一 前期准备 1.1 基础知识 Heketi提供了一个RESTful管理界面,可以用来管理GlusterFS卷的生命周期.Heketi会动态在集群内选择bricks构建所需的volumes,从而确保数 ...
- 领扣(LeetCode)回文链表 个人题解
请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶:你能否用 O(n) 时间复杂 ...
- usaco training <1.2 Greedy Gift Givers>
题面 Task 'gift1': Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided t ...
- 小白学 Python 爬虫(5):前置准备(四)数据库基础
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...