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的更多相关文章

  1. [Javascript] Manage Application State with Immutable.js

    Learn how Immutable.js data structures are different from native iterable Javascript data types and ...

  2. [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 ...

  3. [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 ...

  4. React 全新的 Context API

    Context API 可以说是 React 中最有趣的一个特性了.一方面很多流行的框架(例如react-redux.mobx-react.react-router等)都在使用它:另一方面官方文档中却 ...

  5. react中对于context的理解

    一.context旧版的基本使用 1.context的理解 当不想在组件树中通过逐层传递props或state的方式来传递数据时,可使用context来实现跨层级的组件数据传递. 2.context的 ...

  6. React之使用Context跨组件树传递数据

    ---------------------------------  讲解一 原文:https://blog.csdn.net/xuxiaoping1989/article/details/78480 ...

  7. react中的context的基础用法

    context提供了一种数据共享的机制,里面有两个关键概念——provider,consumer,下面做一些key features描述. 参考网址:https://react.docschina.o ...

  8. React.js 的 context

    这一节我们来介绍一个你可能永远用不上的 React.js 特性 —— context.但是了解它对于了解接下来要讲解的 React-redux 很有好处,所以大家可以简单了解一下它的概念和作用. 在过 ...

  9. React 深入系列3:Props 和 State

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列3:Props 和 State React 深入系列,深入讲解了React中的重点概念.特性和模式 ...

随机推荐

  1. 【hdu 6181】Two Paths

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6181 [题意] 让你求从1到n的次短路 [题解] 模板题; 因为点可以重复走; 则一定会有次短路. di ...

  2. python内存增长问题

    如果你的程序没有调用什么特殊的库, 只是用了很平常的库, 而且使再循环很多的情况下, 那么建议你把循环里的程序拆出来,写成一子函数,循环子函数. 如下面格式: for   (循环) 子函数 这样程序每 ...

  3. kindle paperwhite 简单笔记按名称分类

    已更新python,见新博客  http://www.hrwhisper.me/archives/708 写作背景: 南京决赛比赛完那天晚上写的. 使用方法: 将My Clippings.txt 放在 ...

  4. 【例题 7-13 UVA-1374】Power Calculus

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 结论:每次只用新生成的数字就好了. 然后就是IDA*了. 迭代深搜+剪枝. [代码] /* 1.Shoud it use long ...

  5. echarts同一页面四个图表切换的js数据交互

    需求:点击tab页,切换四个不同的图表,ajax向后台请求数据,展示在四个不同的图表中. 其余的就不多说,直接上js代码了 $(function() { $("#heart").o ...

  6. 【例题 6-16 UVa 10129】Play on Words

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 入度减去出度个数为1的点和为-1的点各有1个,然后其他点入度减去出度为0 或者全都是入度为0的点即可. [代码] #include ...

  7. [Angular] AuthService and AngularFire integration

    Config AngularFire, we need database and auth module from firebase. import {NgModule} from '@angular ...

  8. [Angular] Learn How To Use ng-template Inputs

    For example, we have a modal component, it can config that using ng-template as a configurable templ ...

  9. 记一次内存泄漏调试(memory leak)-Driver Monkey

    Author:DriverMonkey Mail:bookworepeng@Hotmail.com Phone:13410905075 QQ:196568501 硬件环境:AM335X 软件环境:li ...

  10. php实现字符串替换

    php实现字符串替换 一.总结 二.php实现字符串替换 代码一: //字符串替换 function str_replace($substr , $newsubstr, $str) { $m = st ...