[Redux] Generating Containers with connect() from React Redux (VisibleTodoList)
Learn how to use the that comes with React Redux instead of the hand-rolled implementation from the previous lesson.
Code to be refactored:
class VisibleTodoList extends Component {
componentDidMount() {
const { store } = this.context;
this.unsubscribe = store.subscribe(() =>
this.forceUpdate()
);
} componentWillUnmount() {
this.unsubscribe();
} render() {
const props = this.props;
const { store } = this.context;
const state = store.getState(); return (
<TodoList
todos={
getVisibleTodos(
state.todos,
state.visibilityFilter
)
}
onTodoClick={id =>
store.dispatch({
type: 'TOGGLE_TODO',
id
})
}
/>
);
}
} VisibleTodoList.contextTypes = {
store: React.PropTypes.object
};
In the code, we handle the 'context', subscribe state and unsubscribe. Also we need to always remember write 'contextTypes'. There are lots of things todo.
Actually we can use 'ReactRedux' libaray to simpy our life:
'ReactRedux' libaray's connect() fucntion also pass the context down to the component.
VisibleTodoList:
So get the todos and write into mapStateToProps function:
const mapStateToProps = (state) => {
return {
todos: getVisibleTodos(
state.todos,
state.visibilityFilter
)
};
};
then get onTodoClick dispatch function to the mapDispatchToProps function:
const mapDispatchToProps = (dispatch) => {
return {
onTodoClick: (id) => {
dispatch({
type: 'TOGGLE_TODO',
id
});
}
};
};
then we can use 'connect()()' function to connect state, dispatch action and Render component together:
const { connect } = ReactRedux;
const VisibleTodoList = connect(
mapStateToProps,
mapDispatchToProps
)(TodoList);
-------------------------
code:
const mapStateToProps = (state) => {
return {
todos: getVisibleTodos(
state.todos,
state.visibilityFilter
)
};
}; const mapDispatchToProps = (dispatch) => {
return {
onTodoClick: (id) => {
dispatch({
type: 'TOGGLE_TODO',
id
});
}
};
}; const { connect } = ReactRedux;
const VisibleTodoList = connect(
mapStateToProps,
mapDispatchToProps
)(TodoList);
[Redux] Generating Containers with connect() from React Redux (VisibleTodoList)的更多相关文章
- [Redux] Generating Containers with connect() from React Redux (FooterLink)
Code to be refactored: class FilterLink extends Component { componentDidMount() { const { store } = ...
- [Redux] Generating Containers with connect() from React Redux (AddTodo)
Code to be refacted: const AddTodo = (props, { store }) => { let input; return ( <div> < ...
- 使用react+redux+react-redux+react-router+axios+scss技术栈从0到1开发一个applist应用
先看效果图 github地址 github仓库 在线访问 初始化项目 #创建项目 create-react-app applist #如果没有安装create-react-app的话,先安装 npm ...
- React Redux Sever Rendering实战
# React Redux Sever Rendering(Isomorphic JavaScript) connect、applyMiddleware、thunk、webpackHotMiddleware
今天,我们通过解读官方示例代码(counter)的方式来学习react+redux. 例子 这个例子是官方的例子,计数器程序.前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一( ...
- react+redux教程(五)异步、单一state树结构、componentWillReceiveProps
今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...
- react+redux教程(四)undo、devtools、router
上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...
- react+redux+generation-modation脚手架添加一个todolist
当我遇到问题: 要沉着冷静. 要管理好时间. 别被bug或error搞的不高兴,要高兴,又有煅炼思维的机会了. 要思考这是为什么? 要搞清楚问题的本质. 要探究问题,探究数据的流动. TodoList ...
- 实例讲解基于 React+Redux 的前端开发流程
原文地址:https://segmentfault.com/a/1190000005356568 前言:在当下的前端界,react 和 redux 发展得如火如荼,react 在 github 的 s ...
随机推荐
- using namespace cocos2d;
忘记在头文件添加using namespace cocos2d; 导致一直出现问题,定义的精灵却一直报错. error C2143: 语法错误 : 缺少“;”(在“*”的前面)
- Android 基于Netty的消息推送方案之字符串的接收和发送(三)
在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...
- LDAP-常用命令
1.recreating default ads instance ./ [root@dhcppc2 ~]# dsadm delete /usr/local/dsee7/var/dcc/ads #de ...
- hdu 1301
最小生成树模板题 简单的prim算法 AC代码: #include <iostream> #include <stdio.h> #define INF 9999999 usin ...
- 转载——SqlServer之like、charindex、patindex
转载自:http://www.2cto.com/database/201305/214967.html SqlServer之like.charindex.patindex 1.环境介绍 测试环境 ...
- shell获取文件行数
获取文件行数: echo `cat $file | wc -l` 获取文件中不重复的行数(去重后) echo `awk '{$1="";print $0;}' $file_tel ...
- 使用UISegementControl实现简易汤姆猫程序
// // TomViewController.m #import "TomViewController.h" #import <AVFoundation/AVFoundat ...
- 多维背包 hrbudt 1335 算法与追MM
hrbust #include<string.h> //多进制储存数,第i位进制维back[i]+1,可以避免重复 #include<stdio.h> using namesp ...
- [目录][总结] C++和Java 中的主要操作对比
总结一些,C++ 和Java 中的一些常用操作对比,就当是自己的查询工具书啦.(暂时按随笔的更新时间排序) [Stack] c++ V.S. Java (2015.04.27) [Map] c++ ...
- Ping命令详解
引言:我们每天都在使用Ping命令,但是我们可能不太清楚Ping的工作原理,对运行结果中的很多细节也不是很清楚.查找了一下资料,现在和大家分享一下Ping的运行原理和相关细节. A.Ping命令的工作 ...