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

  1. [Redux] Generating Containers with connect() from React Redux (FooterLink)

    Code to be refactored: class FilterLink extends Component { componentDidMount() { const { store } = ...

  2. [Redux] Generating Containers with connect() from React Redux (AddTodo)

    Code to be refacted: const AddTodo = (props, { store }) => { let input; return ( <div> < ...

  3. 使用react+redux+react-redux+react-router+axios+scss技术栈从0到1开发一个applist应用

    先看效果图 github地址 github仓库 在线访问 初始化项目 #创建项目 create-react-app applist #如果没有安装create-react-app的话,先安装 npm ...

  4. React Redux Sever Rendering实战

    # React Redux Sever Rendering(Isomorphic JavaScript) ![React Redux Sever Rendering(Isomorphic)入门](ht ...

  5. react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware

    今天,我们通过解读官方示例代码(counter)的方式来学习react+redux. 例子 这个例子是官方的例子,计数器程序.前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一( ...

  6. react+redux教程(五)异步、单一state树结构、componentWillReceiveProps

    今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...

  7. react+redux教程(四)undo、devtools、router

    上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...

  8. react+redux+generation-modation脚手架添加一个todolist

    当我遇到问题: 要沉着冷静. 要管理好时间. 别被bug或error搞的不高兴,要高兴,又有煅炼思维的机会了. 要思考这是为什么? 要搞清楚问题的本质. 要探究问题,探究数据的流动. TodoList ...

  9. 实例讲解基于 React+Redux 的前端开发流程

    原文地址:https://segmentfault.com/a/1190000005356568 前言:在当下的前端界,react 和 redux 发展得如火如荼,react 在 github 的 s ...

随机推荐

  1. Android Studio使用教程(一)

    今年的Google全球开发者大会虽然没有新的Android系统和设备,但是还是推出了一些不错的产品,Android Studio就是其中之一.这个基于Intellij IDEA开发的Android I ...

  2. Python对文件的操作(转)

    一.文件对象 我理解的文件对象就是一个接口,通过这个接口对文件进行相关操作. <Python 核心编程>上说的很晦涩,这里没有深刻理解到,希望有人能解释给我听. >>> ...

  3. SQL数值函数

    /*abs(n)返回参数n所指定数值的绝对值(如果参数值为NULL,则返回结果为NULL,下同).*/--SELECT ABS(-3.14) FROM DUAL; --3.14 /*round(n[, ...

  4. 内容提供者 ContentResolver 数据库 示例 -1

    MainActivity public class MainActivity extends ListActivity {     private TextView tv_info;     priv ...

  5. 【回顾整理】HTML+CSS个的两个实战项目

    一:麦子商城首页制作 代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset= ...

  6. ASP.NET MVC上传文件的几种方法

    1.Form表单提交 <p>Form提交</p> <form action="@Url.Action("SavePictureByForm" ...

  7. java学习笔记(3):java的工作原理及相关基础

    一.运行机制 如上图所示,图中内容即为Java的运行机制: 1.我们一开始所编写的代码文件存储格式为(如text.java)文件,这就是源程序文件 2.在Java编辑器的作用下,也就是就行了编译,形成 ...

  8. (原)torch的apply函数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6221633.html torch中的apply函数通过可以不断遍历model的各个模块.实际上其使用的 ...

  9. linux 输入输出重定向

    输入输出重定向 1,输入输出重定向,是针对过滤器的,不针对,编辑器和交互工具 2,>号只把正确的标准输出重定向,输出错误信息,可以用2> 3,新建或清空文件可以直接用>filenam ...

  10. Sql数据保存到Excel文件中

    public string ExportExcel( DataSet ds,string saveFileName) { try { if (ds == null) return "数据库为 ...