[Redux] Filtering Redux State with React Router Params
We will learn how adding React Router shifts the balance of responsibilities, and how the components can use both at the same time.
Now when we click the filter, the url changes, but the todos list doesn't change. So continue with that.
Router only render the App component, and App component will get a props called 'params' contains filter object and we pass this filter to VisibleTodoList:
// App.js import React from 'react';
import Footer from './Footer';
import AddTodo from './AddTodo';
import VisibleTodoList from './VisibleTodoList'; const App = ({params}) => (
<div>
<AddTodo />
<VisibleTodoList filter={params.filter}/>
<Footer />
</div>
); export default App;
In VisibleTodoList.js, we change the mapStateToProps function, add a param 'ownProps' which get from App.js. It contains the 'filter' proporty.
Then in 'getVisibleTodos()' function, change the switch case to match router.
// VisibleTodoList.js
import { connect } from 'react-redux';
import { toggleTodo } from '../actions';
import TodoList from './TodoList';
const getVisibleTodos = (todos, filter) => {
  switch (filter) {
    case 'all':
      return todos;
    case 'completed':
      return todos.filter(t => t.completed);
    case 'active':
      return todos.filter(t => !t.completed);
    default:
      throw new Error(`Unknown filter: ${filter}.`);
  }
};
const mapStateToProps = (state, ownProps) => {
  return {
    todos: getVisibleTodos(state.todos, ownProps.filter),
  };
};
const mapDispatchToProps = (dispatch) => {
  return {
    onTodoClick: (id) => {
      dispatch(toggleTodo(id));
    },
  };
};
const VisibleTodoList = connect(
  mapStateToProps,
  mapDispatchToProps
)(TodoList);
export default VisibleTodoList;
Last, change the devServer.js, app.get('/*'), any router will return index.html.
import path from 'path';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import config from './webpack.config.babel';
import Express from 'express'; const app = new Express();
const port = 3000; const compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
})); app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
}); app.listen(port, error => {
/* eslint-disable no-console */
if (error) {
console.error(error);
} else {
console.info(
'[Redux] Filtering Redux State with React Router Params的更多相关文章
- [Redux] Adding React Router to the Project
 We will learn how to add React Router to a Redux project and make it render our root component. Inst ...
- 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)
 请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...
- [Redux] Navigating with React Router <Link>
 We will learn how to change the address bar using a component from React Router. In Root.js: We need ...
- 关于props和state以及redux中的state
 React的数据模型分为共有数据和私有数据,共有数据可以在组件间进行传递,私有数据为当前组件私有.共有数据在React中使用props对象来调用,它包含标签所有的属性名称和属性值,props对象有三个 ...
- Redux教程2:链接React
 通过前面的教程,我们有了简单的环境,并且可以运行Redux的程序,也对 如何编写Redux示例 有了初步的印象: 掌握了 使用Redux控制状态转移 ,继而驱动 React 组件发生改变,这才是学习R ...
- 关于react router 4 的小实践
 详细代码栗子:https://github.com/wayaha/react-dom-CY clone然后 npm install npm start 分割线 1.这个项目使用create-react ...
- React Router 4.x 开发,这些雷区我们都帮你踩过了
 前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...
- React Router API文档
 React Router API文档 一.<BrowserRouter> 使用HTML5历史记录API(pushState,replaceState和popstate事件)的<Rou ...
- [Web 前端] React Router v4 入坑指南
 cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...
随机推荐
- UIlabel - 富文本属性
 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...
- 如何跳到系统设置界面-b
 NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"]; if ([[UIApplication sharedApplicati ...
- hdu 3394 Railway
 这是一道用tarjin求双连通分量的题: 其中,不需要修的道路就是桥的数目: 在图的每个极大环中,如果点的数目小于边的数目,显然这个环中含有子环,并且这个环的边数就是这个环中有冲突的边的数目: 如果点 ...
- 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)
 [题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...
- VS(Microsoft Visual Studio2010)工具打开项目所需的应用程序,出现未安装(.csproj)的应用程序的解决办法
 打开这个文件的话,从列表中选择打开方式的时候,看看选择打开项目文件的打开方式是不是visual studio,如果是Micrisoft visual studio version selector.那 ...
- 【HDOJ】2546 饭卡
 01背包,需要先对数据升序排序.这样保证优先购买最贵的东西,才满足背包条件. #include <stdio.h> #include <string.h> #include & ...
- VM Depot 助您使用本地开源软件架设开发 Web 站点
  发布于 2014-04-25 作者 云 浪生 使用 VM Depot 中的镜像在 Azure 上创建.开发.部署网站与应用不仅方便快捷而且省时省力!感谢开源社区的大力支持,我们的VM Depot ...
- selenium webdriver(6)---cookie相关操作
 介绍selenium操作cookie之前,先简单介绍一下cookie的基础知识 cookie cookie一般用来识别用户身份和记录用户状态,存储在客户端电脑上.IE的cookie文件路径(win7) ...
- ASPNETMVC多语言方案
 ASPNETMVC多语言方案 前言: 好多年没写文章了,工作很忙,天天加班, 每天都相信不用多久,就会升职加薪,当上总经理,出任CEO,迎娶白富美,走上人生巅峰,想想还有点小激动~~~~ 直到后来发生 ...
- [App]Xamarin First(Or Last One) App
 这个应用简单得无以复加,主要是熟悉了使用Xamarin Studio进行Android开发的配置和基本流程. 以前未曾具体得做过App开发,现在大致了解了开发所包含的基本元素. 如上图,在Layout ...