[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)">&times;</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.

时间: 2024-10-02 17:28:52

[AngualrJS NG-redux] Map State and Dispatchers to Redux的相关文章

react+redux教程(七)自定义redux中间件

教程源代码及目录 https://github.com/lewis617/myReact 今天,我们要讲解的是自定义redux中间件这个知识点.本节内容非常抽象,特别是中间件的定义原理,那多层的函数嵌套和串联,需要极强逻辑思维能力才能完全消化吸收.不过我会多罗嗦几句,所以不用担心. 例子 例子是官方的例子real-world,做的是一个获取github用户.仓库的程序. 本例子源代码: https://github.com/lewis617/myReact/tree/master/redux-e

[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 reset our state at any time by dispatching an action. We can get the best of both worlds by having a function that will return an object with all of our

redux+flux(一:入门篇)

React是facebook推出的js框架,React 本身只涉及UI层,如果搭建大型应用,必须搭配一个前端框架.也就是说,你至少要学两样东西,才能基本满足需要:React + 前端框架. Facebook官方使用的是 Flux 框架.本文就介绍如何在 React 的基础上,使用 Flux 组织代码和安排内部逻辑. 首先,Flux将一个应用分成四个部分: Flux 的最大特点,就是数据的"单向流动". 用户访问 View View 发出用户的 Action Dispatcher 收到

redux异步操作学习笔记

摘要: 发觉在学习react的生态链中,react+react-router+webpack+es6+fetch等等这些都基本搞懂的差不多了,可以应用到实战当中,唯独这个redux还不能,学习redux还学的挺久的. 其中困扰我最久的就是redux的异步数据流的处理.难点主要是概念太多,接触的词太多,而且网上的案例看的头都疼,很容易晕,已经晕了好多次了.后来被我简化之后,终于搞懂了,哈哈.!来来来,今天总结一下,希望对大家有所帮助.不过本人主要是介绍redux的异步操作,如果对redux不是很熟

React、Redux 和 Bootstrap

使用 React.Redux 和 Bootstrap 实现 Alert 今天,我们来学习使用 React.Redux 和 Bootstrap 实现Alert. 例子 这个例子实现了弹出不同类型信息的功能,这些信息默认会在5秒后消失,你也可以手动点击使其消失.如果你在服务端有信息要提示,还可以通过 Redux 的单一数据源传到客户端在渲染页面时显示出来. 源代码: https://github.com/lewis617/react-redux-tutorial/tree/master/r2-bs-

[Redux] Colocating Selectors with Reducers

We will learn how to encapsulate the knowledge about the state shape in the reducer files, so that the components don’t have to rely on it. In current VisibleTodoList.js: import { connect } from 'react-redux'; import { withRouter } from 'react-router

Redux系列x:源码解析

写在前面 redux的源码很简洁,除了applyMiddleware比较绕难以理解外,大部分还是 这里假设读者对redux有一定了解,就不科普redux的概念和API啥的啦,这部分建议直接看官方文档. 此外,源码解析的中文批注版已上传至github,可点击查看.本文相关示例代码,可点击查看. 源码解析概览 将redux下载下来,然后看下他的目录结构. npm install redux 这里我们需要关心的主要是src目录,源码解析需要关心的文件都在这里面了 index.js:redux主文件,主

redux源码解析,函数式编程

提到redux,会想到函数式编程.什么是函数式编程?是一种很奇妙的函数式的编程方法.你会感觉函数式编程这么简单,但是用起来却很方便很神奇. 在<functional javascript>中,作者批评了java那种任何东西都用对象来写程序的方式,提倡了这种函数式编程. 之前看过一些函数式编程的例子(以下简称fp).提到fp会想到underscore和lodash,你会看到lodash的包中,唯一一个文件夹就是fp,里面是fp相关的函数. 在redux中,也是运用了很多fp的函数.其实在写js中

Redux和React-Redux的实现(三):中间件的原理和applyMiddleware、Thunk的实现

现在我们的Redux和React-Redux已经基本实现了,在Redux中,触发一个action,reducer立即就能算出相应的state,如果我要过一会才让reducer计算state呢怎么办?也就是我们如何实现异步的action呢?这里就要用到中间件(middleware) 1. 中间件(middleware)介绍 中间就是在action与reducer之间又加了一层,没有中间件的Redux的过程是:action -> reducer,而有了中间件的过程就是action -> middl