[AngualrJS NG-redux] Map State and Dispatchers to Redux
In this lesson, we are going to learn how to map our Angular component directly to our application store using the connect method on ngRedux.
In Angular, it is a common technique to bind a controller property directly to the model so that our controllers remain lightweight and operate more as a pass through mechanism than anything else.
Using the connect method, we can accomplish the same effect by not only binding properties directly to the application store, but also binding our controller to our action creators. This allows us to drastically simplify our controllers as we are able to delete a bunch of local controller methods as they become unnecessary because of our mappings.
connect(mapStateToThis, actions)(context):
p1: mapStateToThis: function(state),
p2: actions: object --> ActionsCreators
p3: Context
Basiclly connect do two things:
1. Exports the actions to the controller, so you don't need to do like this:
this.store.dispatch(this.CategoriesActions.getCategoreis());
Instead you can do :
this.getCategoreis();
2. You don't need to subscribe to the store manully anymore, it will automaticlly subscribe to store:
So instead doing like this:
this.unsubscribe = this.store.subscribe(() => {
this.categories = this.store.getState().categories;
this.currentCategory = this.store.getState().category;
});
Now you can do this:
this.unsubscribe = this.store.connect(this.mapStateToThis, actions)(this);
The connect function also return a unsubscribe function which you can call in $onDestroy life cycle.
mapStateToThis:
mapStateToThis(state) {
return {
categories: state.categories,
currentCategory: state.currentCategory
};
}
Basiclly it will replace:
this.unsubscribe = this.store.subscribe(() => {
this.categories = this.store.getState().categories;
this.currentCategory = this.store.getState().category;
});
actions:
const actions = Object.assign({}, this.CategoriesActions, this.BookmarksActions);
this.unsubscribe = this.store.connect(this.mapStateToThis, actions)(this);
Actions is an object contains the actions creators you need for this controller.
Other benefits:
So for example you have a function called 'deleteBookmark' on the controller. And inside this function, you call 'this.BookmarksActions.deleteBookmark(bookmark)' function.
Two functions' name are the same, then you can get rid of 'deleteBookmark' the whole function.
<button type="button" class="close" ng-click="bookmarksListCtrl.deleteBookmark(bookmark)">×</button>
/*
// The whole function can be removed
deleteBookmark(bookmark) {
this.store.dispatch(
this.BookmarksActions.deleteBookmark(bookmark)
)
}*/
Becasue connect function already exprots the 'deletedBookmark' function to the ctrl.
[AngualrJS NG-redux] Map State and Dispatchers to Redux的更多相关文章
- react中界面跳转 A界面跳B界面,返回A界面,A界面状态保持不变 redux的state方法
在上一篇文章中说过了react中界面A跳到B,返回A,A界面状态保持不变,上篇中使用的是传统的localStorage方法,现在来使用第二种redux的state方法来实现这个功能 现在我刚接触red ...
- 【React】360- 完全理解 redux(从零实现一个 redux)
点击上方"前端自习课"关注,学习起来~ 前言 记得开始接触 react 技术栈的时候,最难理解的地方就是 redux.全是新名词:reducer.store.dispatch.mi ...
- react+redux教程(七)自定义redux中间件
今天,我们要讲解的是自定义redux中间件这个知识点.本节内容非常抽象,特别是中间件的定义原理,那多层的函数嵌套和串联,需要极强逻辑思维能力才能完全消化吸收.不过我会多罗嗦几句,所以不用担心. 例子 ...
- Redux的State不应该全部放在Store里
使用了redux管理应用的状态,应用的状态不应该全部放在Store里面. 前端状态主要有一下两种: 1. Domain data 2. UI State 1. Domain data 来自于服务端对领 ...
- [Functional Programming ADT] Initialize Redux Application State Using The State ADT
Not only will we need to give our initial state to a Redux store, we will also need to be able to re ...
- react脚手架改造(react/react-router/redux/eslint/karam/immutable/es6/webpack/Redux DevTools)
公司突然组织需要重新搭建一个基于node的论坛系统,前端采用react,上网找了一些脚手架,或多或少不能满足自己的需求,最终在基于YeoMan的react脚手架generator-react-webp ...
- redux源码解析(深度解析redux+异步demo)
redux源码解析 1.首先让我们看看都有哪些内容 2.让我们看看redux的流程图 Store:一个库,保存数据的地方,整个项目只有一个 创建store Redux提供 creatStore 函数来 ...
- Immutable.js 以及在 react+redux 项目中的实践
来自一位美团大牛的分享,相信可以帮助到你. 原文链接:https://juejin.im/post/5948985ea0bb9f006bed7472?utm_source=tuicool&ut ...
- 谈谈React那些小事
前言 说起React,那也是近一年多时间火起来的前端框架,其在Facebook的影响力和大力推广下,已然成为目前前端界的中流砥柱.在如今的前端框架界,React.Vue.Angular三分天下的时代已 ...
随机推荐
- logname---显示用户名称
logname命令用来显示用户名称.
- python django的单元测试
# TestCase 基类的使用 django.test.TestCase # 测试依赖于数据库访问,创建或查询模型 unittest.TestCase #没有与数据库交互 避免刷新数据库的成本
- 【Python学习】爬虫报错处理bs4.FeatureNotFound
[BUG回顾] 在学习Python爬虫时,运Pycharm中的文件出现了这样的报错: bs4.FeatureNotFound: Couldn’t find a tree builder with th ...
- HDU——T 3336 Count the string
http://acm.hdu.edu.cn/showproblem.php?pid=3336 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- php如何读写excel
php如何读写excel 一.总结 一句话总结:PHP操作Excel最好的方法是使用PHPExcel类, 可以到官网下载PHPExcel类库 http://phpexcel.codeplex.com ...
- 28.Node.js 函数和匿名函数
转自:http://www.runoob.com/nodejs/nodejs-module-system.html 在JavaScript中,一个函数可以作为另一个函数的参数.我们可以先定义一个函数, ...
- WebService 的Description 属性说明(转)
转自:http://exception.thinksaas.cn/0/173/173623.html 在WebMethod的description 中可使用超文本, 举例: 如上图中,红框类的WebS ...
- VMware Tools安装问题的解决
一.VMware Tools工具的作用 VMware虚拟机的插件工具,安装上它可以实现主机与虚拟机的文件共享及拖放.简单的说就是从Ubuntu上边经常输入命令行会出现错误的情况,需要复制到Window ...
- Java Drp项目实战——Servlet
由来 在解说Servlet之前须要先介绍一个词语CGI即Common GatewayInterface是通用网关接口的意思.它提供一个计算机程序同HTTP协议或者WWW服务的接口,也就是人机交互接口的 ...
- [Python] Object spread operator in Python
In JS, we have object spread opreator: const x = { a: '1', b: '2' } const y = { c: '3', d: '4' } con ...