[React] Use React Context to Manage Application State Through Routes
We’ll create a Router component that will wrap our application and manage all URL related state. We’ll see how we can use React’s built in context mechanism to pass data and functions between components without having to pass props all the way down through the component tree.
// index.js ReactDOM.render(
<MuiThemeProvider>
<Router><App /></Router>
</MuiThemeProvider>, document.getElementById('root'));
On the top level, we add Router component to wrap our App component.
export class Router extends Component {
state = {
route: getCurrentPath()
}; handleLinkClick = (route) => {
this.setState({route});
history.pushState(null, '', route);
}; static childContextTypes = {
route: React.PropTypes.string,
linkHandler: React.PropTypes.func
}; getChildContext() {
return {
route: this.state.route,
linkHandler: this.handleLinkClick
};
} render() {
return (
<div>{this.props.children}</div>
);
}
}
We need to pass the props to the children so that Link component can know current url state. To do this, we using 'Context' instead of 'passing the props down to children'.
Becasue there are two problems with this. One, in a complex app, that could potentially mean passing the same item down many levels. This could mean a lot of maintenance if things need to change.
The second problem is that, in this setup, app is being placed inside the router through a call to this.props.children. We can't just add props onto the app component in our render function. The way we're going to handle this is through React's context mechanism.
import React, {Component} from 'react'; const styles = {
padding: '8px'
}; export class Link extends Component { static contextTypes = {
route: React.PropTypes.string,
linkHandler: React.PropTypes.func
}; render() {
const activeClass = this.context.route === this.props.to ? 'active': '';
const handleClick = (ev) => {
ev.preventDefault();
this.context.linkHandler(this.props.to);
}; return (
<a href="#" style={styles} className={activeClass} onClick={handleClick}>{this.props.children}</a>
);
}
} Link.PropTypes = {
to: React.PropTypes.string.isRequired
};
Last, in the Link component, we can use the Context to access what we have defined for Router compoent.
[React] Use React Context to Manage Application State Through Routes的更多相关文章
- [Javascript] Manage Application State with Immutable.js
Learn how Immutable.js data structures are different from native iterable Javascript data types and ...
- [React] Keep Application State in Sync with Browser History
Using pushState and passing route data via context allows our application to respond to route change ...
- [React] Update Application State with React Apollo ApolloConsumer Component
In this lesson I refactor some code that utilizes the Mutation component to update client-side cache ...
- React 全新的 Context API
Context API 可以说是 React 中最有趣的一个特性了.一方面很多流行的框架(例如react-redux.mobx-react.react-router等)都在使用它:另一方面官方文档中却 ...
- react中对于context的理解
一.context旧版的基本使用 1.context的理解 当不想在组件树中通过逐层传递props或state的方式来传递数据时,可使用context来实现跨层级的组件数据传递. 2.context的 ...
- React之使用Context跨组件树传递数据
--------------------------------- 讲解一 原文:https://blog.csdn.net/xuxiaoping1989/article/details/78480 ...
- react中的context的基础用法
context提供了一种数据共享的机制,里面有两个关键概念——provider,consumer,下面做一些key features描述. 参考网址:https://react.docschina.o ...
- React.js 的 context
这一节我们来介绍一个你可能永远用不上的 React.js 特性 —— context.但是了解它对于了解接下来要讲解的 React-redux 很有好处,所以大家可以简单了解一下它的概念和作用. 在过 ...
- React 深入系列3:Props 和 State
文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列3:Props 和 State React 深入系列,深入讲解了React中的重点概念.特性和模式 ...
随机推荐
- 【2017 Multi-University Training Contest - Team 6】Inversion
[链接]h在这里写链接 [题意] 给出一个序列,求2~n每一个数,下标不是这个数倍数的最大值是什么? [题解] 把a数组从大到小排序. 每个位置i,逆序枚举b数组,找到第一个对应下标不是i的倍数的,直 ...
- Spark源代码分析之中的一个:Job提交执行总流程概述
Spark是一个基于内存的分布式计算框架.执行在其上的应用程序,依照Action被划分为一个个Job.而Job提交执行的总流程.大致分为两个阶段: 1.Stage划分与提交 (1)Job依照RDD之间 ...
- spring 通过编程来获取属性文件
配置可以读取属性: <beans profile="dev"> <context:property-placeholder ignore-resource-not ...
- VC 常见问题百问
http://www.cnblogs.com/cy163/archive/2006/06/19/429796.html 经典Vc书 Charles Petzold 的<Programming W ...
- QQ在线交谈代码
非常多商业站点的右边都会有一个固定或者浮动的层显示QQ在线在线交谈或者咨询的button.当浏览者点击了就会弹出相应的对话框. 这里的QQ交谈有两种: 一种是企业QQ,那要生成以上的功能就非常easy ...
- 第一个Python程序(全面)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一.Windows系统 1.编写Python程序方式之Sublime文本编辑器: 1>打开sublime,创建hello.p ...
- Oracle10g中阻塞锁查询更简单
http://blog.itpub.net/195110/viewspace-677572/ http://blog.sina.com.cn/s/blog_636415010100khcl.html
- php模拟顺序栈基本操作
php模拟顺序栈基本操作 一.总结 写函数什么永远记住边界情况:比如 echo "栈已满!<br/>" ; 栈已空这种,那就能多考虑几种情况,代码就很正宗了 1.对象 ...
- php 获取隔日日期
php获取日期时间 <?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d&qu ...
- 【例题 6-5 UVA 12657 】Boxes in a Line
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 双向链表模拟题. 移动的时候,要注意它就在所需要的位置的情况.那种情况不移动. (如果已经在所需位置了,还用链表的插入方式强行移动的 ...