A component author has no way of knowing which state changes a consumer will want to override, but state reducers handle this problem by allowing the parent component to override any state change.

In Short, we want to have a way to override the component's internal state from outside. The reason for that is there maybe some requirements from the users who want to override component internal state for whatever reason.

The state reducer can accomplish this task, so what is state reducer? It is just a fansic name form some smart developers. State reducer is a function which two two params, one is old state, another one is changes going to be happen, return value is the new state.

export type ToggleStateReducer =
(state: ToggleState, changes: Partial<ToggleState>)
=> ToggleState;

So inside toggle.component.ts, we accept an @Input() stateReducer:

  @Input() stateReducer: ToggleStateReducer =
(state, changes) => ({ ...state, ...changes });

Whenenver we need to change toggle component internal state, we call stateReducer:

  setOnState(on: boolean) {
const oldState = { on: this.on };
const newState = this.stateReducer(oldState, { on });
if (oldState !== newState) {
this.on = newState.on;
this.toggled.emit(this.on);
}
}

That's all what we need to for component part. Just make stateReducer as a input, call each time we need to udpate our internal state, we call thought stateReducer to get new state.

So now, from the consumer component, we can control the state update:

// app.component.ts:

  stateReducer = (state: ToggleState, changes: Partial<ToggleState>) => {
if (this.timesClicked > ) {
return state;
}
if (changes.on !== undefined) {
this.timesClicked = this.timesClicked + ;
}
return { ...state, ...changes };
}
<toggle (toggled)="log('toggle', $event)" [stateReducer]="stateReducer">
<ng-template let-on="on" let-fns="fns">
<switch [on]="on" toggler (click)="fns.toggle()">
</switch>
</ng-template>
</toggle>

[Angular] Expose Angular Component Logic Using State Reducers的更多相关文章

  1. Exploring the Angular 1.5 .component() method

    Angular 1.5 introduced the .component() helper method, which is much simpler than the.directive() de ...

  2. [Angular] Use Angular components in AngularJS applications with Angular Elements

    When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...

  3. angular 2 angular quick start Could not find HammerJS

    Angular2 的material中 引用了 hammerjs,遇到Could not find HammerJS错误,正确的步骤如下: 需要在如下位置增加 对material 和 hammerjs ...

  4. ASP.NET Core 2.1 Web API + Identity Server 4 + Angular 6 + Angular Material 实战小项目视频

    视频简介 ASP.NET Core Web API + Angular 6的教学视频 我是后端开发人员, 前端的Angular部分讲的比较差一些, 可以直接看代码!!!! 这是一个小项目的实战视频, ...

  5. [Angular] Refactor Angular Component State Logic into Directives

    Allow the base toggle to be a tag (<toggle>) or attribute (<div toggle>). The <toggle ...

  6. [Angular] Two ways to create Angular Animation, using animation() or using state()

    We have two blocks to show to difference ways to do animation in Angular: <button (click)="t ...

  7. Angular(二) - 组件Component

    1. 组件Component示例 2. Component常用的几个选项 3. Component全部的选项 3.1 继承自@Directive装饰器的选项 3.2 @Component自己特有的选项 ...

  8. [Angular 2] Passing Template Input Values to Reducers

    Angular 2 allows you to pass values from inputs simply by referencing them in the template and passi ...

  9. [Angular] Use Angular’s @HostBinding and :host(...) to add styling to the component itself

    One thing that we can do is to add styles directly to HTML elements that live within our component. ...

随机推荐

  1. Chrome安装助手踩坑

    [前言] 最近用之前的方法配置hosts,想浏览下载国外网站的数据和插件,突然发现几乎所有的方法都无效了...... 本文介绍下下载谷歌助手,通过助手访问国外网站 [主体] (1)搜索谷歌助手,点击下 ...

  2. No-6.用户权限相关命令

    用户权限相关命令 目标 用户 和 权限 的基本概念 用户管理 终端命令 组管理 终端命令 修改权限 终端命令 01. 用户 和 权限 的基本概念 1.1 基本概念 用户 是 Linux 系统工作中重要 ...

  3. luogu P2865 路障

    https://www.luogu.org/problemnew/show/P2865 看到题解好多dijkstra,作为一名钟爱于spfa的蒟蒻看不下去了. 有些spfa要跑两边,代码量要曾长好多( ...

  4. poj-1163 动态规划

    这道题目并不能直接使用递归,因为 7(1) 7(1)         7(1) 7(1)      7(2)         7(1) 7(1)       7(3)         7(3)    ...

  5. centos7 rsync+inotify软件实现集群服务的数据备份(一)

    一.rsync软件的说明: 1.1 什么是rsync rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.它使用所谓的“Rsync演算法”来使本地和远程两个主机之间的文件达 ...

  6. (1) openssl基础概念

    1.1 背景知识 对称加密     :加密解密使用同一密钥,加解密速度快.随着人数增多,密钥数量急增n(n-1)/2. 非对称加密 :使用公私钥配对加解密,速度慢.公钥是从私钥中提取出来的,一般拿对方 ...

  7. Linux用户和组的概念

    目 录 第1章 用户和组存在的关系    1 1.1 我们现在所使用的操作系统都是多用户操作系统    1 1.2 id命令查看当前登陆的用户信息    1 1.3 用户UID的分类    1 1.4 ...

  8. oracle如何重做日志组

    1.查询数据库中的重做日志组 SQL> select * from v$log; GROUP# THREAD# SEQUENCE# BYTES BLOCKSIZE MEMBERS ARC STA ...

  9. 05 selenium模块

    selenium模块 selenium selenium是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作. selenium最初是一个自动化测试工具,而爬虫中使 ...

  10. Activiti数据表

    --1:资源库流程规则表SELECT * FROM JEESITE.act_re_deployment --部署信息表SELECT * FROM JEESITE.act_re_model --流程设计 ...