[Angular] Expose Angular Component Logic Using State Reducers
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的更多相关文章
- Exploring the Angular 1.5 .component() method
Angular 1.5 introduced the .component() helper method, which is much simpler than the.directive() de ...
- [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 ...
- angular 2 angular quick start Could not find HammerJS
Angular2 的material中 引用了 hammerjs,遇到Could not find HammerJS错误,正确的步骤如下: 需要在如下位置增加 对material 和 hammerjs ...
- ASP.NET Core 2.1 Web API + Identity Server 4 + Angular 6 + Angular Material 实战小项目视频
视频简介 ASP.NET Core Web API + Angular 6的教学视频 我是后端开发人员, 前端的Angular部分讲的比较差一些, 可以直接看代码!!!! 这是一个小项目的实战视频, ...
- [Angular] Refactor Angular Component State Logic into Directives
Allow the base toggle to be a tag (<toggle>) or attribute (<div toggle>). The <toggle ...
- [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 ...
- Angular(二) - 组件Component
1. 组件Component示例 2. Component常用的几个选项 3. Component全部的选项 3.1 继承自@Directive装饰器的选项 3.2 @Component自己特有的选项 ...
- [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 ...
- [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. ...
随机推荐
- && (and)、||(or) 条件语句
当前面条件满足时,就执行后面的代码 //条件为真时,就执行其中的语句 if($a>0){ $b='This is test'; } //上面的写法太麻烦,可以这样简写 $a>0 & ...
- QT_6_QMainWindow
QMainWindow 1.1. 菜单栏 1.1.1. 只有一个 1.1.2. QMenuBar *bar = MenuBar(); 1.1.3. 设置到窗口中 setMenuBar(bar); 1. ...
- jsp公共头信息的抽取(相对路径的修改)
1,抽取出的公共头信息 <%@ page language="java" contentType="text/html; charset=UTF-8" p ...
- mysql 根据月份查找数据
- 关于Java多线程(JAVA多线程实现的四种方式)
Java多线程实现方式主要有四种:继承Thread类.实现Runnable接口.实现Callable接口通过FutureTask包装器来创建Thread线程.使用ExecutorService.Cal ...
- 记一次Linux系统被入侵的过程
记一次Linux系统被入侵的过程 1. 前期现象 前期现象,宋组那边反应开发环境192.161.14.98这台机器通过公网下载文件,很慢,ping百度丢包严重.因为这台机器是通过楼下adsl拨号上网, ...
- vue-cli中添加使用less
在vue-cli中构建的项目是可以使用less的,但是查看package.json可以发现,并没有less相关的插件,所以我们需要自行安装. 第一步:安装 npm install less less- ...
- Socket通信时服务端无响应,客户端超时设置
背景:在写一个客户端的socket程序,服务端没有返回消息,客户端一直在等待. 目标:我需要设置一个时间,如果超过这个时间客户端自动断开连接.最好是在服务端实现,客户端对我来说不可控.
- java 自动拆箱 自动装箱
自动装箱的定义就是 基本数据类型赋值给包装类型, 拆箱则相反. Integer integer = 122; // 自动装箱 int num = integer; //自动拆箱 想看一下源码是怎么 ...
- 前端基础之JavaScript_1
摘要: JavaScript简介 引入方式 语言规范 JavaScript语言基础 变量声明 数据类型 运算符 流程控制 函数 词法分析 内置对象 一.JavaScript概述 1.ECMAScrip ...