我有一个动作/减速器/组件.在我的一个组件(组件转储)中,我有一个 Select .我可以从我的store 获得关于什么类型的过滤器的信息.我在哪里可以处理它的行动,或减少?
我有一个动作/减速器/组件.在我的一个组件(组件转储)中,我有一个 Select .我可以从我的store 获得关于什么类型的过滤器的信息.我在哪里可以处理它的行动,或减少?
我将项目sortKey和sortKind(asc/desc)保存在Reduxstore 中.
在我的Angular 组件中(我相信对于React也是如此),我得到了一个可观察的存储状态,这样我就可以在UX中显示项目、sortKey和sortOrder.
当用户单击表列以更改排序键(order)时,我将新的键/排序顺序分派给状态的reducer.
然后,reducer执行新的排序,并返回带有更新值的新状态.
因此,组件中的可观察对象会触发一个更新用户体验的事件.
优势:
保持对组件中的逻辑进行排序
通过在状态中保存sortKey和sortKind,如果用户刷新浏览器,您可以精确地恢复UX(我使用Redux LocalStorage进行同步)
由于store 中有已排序的项目,因此只有当用户主动想要排序时,才能执行排序.
当用户可能返回组件时,会记住已排序的项.
My reducer("bizzes"是我的项目列表,我使用Immutable.list来存储项目)
import { List } from 'immutable';
import { IBizz, IBizzState } from './bizz.types';
import { BIZZES_SET, BIZZES_SORT} from 'store/constants';
const SORT_ASC = 'asc';
const SORT_DESC = 'desc';
const defaultSortKey = 'serialNo';
const defaultSortOrder = SORT_ASC;
const INITIAL_STATE: IBizzState = {
bizzes: List([]),
sortKey: defaultSortKey,
sortOrder: defaultSortOrder
};
export function bizzReducer(state: IBizzState = INITIAL_STATE, action: any): IBizzState {
switch (action.type) {
case BIZZES_SET:
return {
bizzes: List(action.payload.bizzes),
sortKey: action.payload.sortKey || defaultSortKey,
sortOrder: action.payload.sortOrder || defaultSortOrder
};
case BIZZES_SORT:
let sortKey = action.payload.sortKey || defaultSortKey;
if(sortKey === state.sortKey) {
state.sortOrder = state.sortOrder === SORT_ASC ? SORT_DESC : SORT_ASC;
}
return {
bizzes: List(state.bizzes.sort( (a, b) => {
if( a[sortKey] < b[sortKey] ) return state.sortOrder === SORT_ASC ? -1 : 1;
if( a[sortKey] > b[sortKey] ) return state.sortOrder === SORT_ASC ? 1: -1;
return 0;
})),
sortKey: sortKey,
sortOrder: state.sortOrder
};
default: return state;
}
}
以及我的组件(我使用Ng2 Redux将存储作为可观察对象):
import { Component, OnInit, OnDestroy, ChangeDetectionStrategy } from '@angular/core';
import { select } from 'store';
import { BizzActions } from 'actions/index';
@Component({
selector: 'bizzlist',
templateUrl: './bizz-list.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class BizzListComponent implements OnInit {
@select([ 'bizzState']) bizzState$;
public sortOrder: string;
public sortKey: string;
public bizzes = [];
private bizzStateSubscription;
constructor(
public bizzActions: BizzActions
) { }
ngOnInit() {
this.bizzStateSubscription = this.bizzState$.subscribe( bizzState => {
this.bizzes = bizzState.bizzes;
this.sortKey = bizzState.sortKey;
this.sortOrder = bizzState.sortOrder;
});
}
ngOnDestroy() {
this.bizzStateSubscription.unsubscribe();
}
public sortBizzes(key) {
this.bizzActions.sortBizzes(key);
}
}
如您所见,我正在使用一个动作(称为BizzActions)来执行实际的Redux调度.你可以在你的组件中完成,但我更喜欢把这些东西分开.以下是我的BizzActions(一项服务):
import { Injectable } from '@angular/core';
import { NgRedux, IAppState } from 'store';
import {
BIZZES_SET,
BIZZES_SORT
} from 'store/constants';
@Injectable()
export class BizzActions {
constructor (private ngRedux: NgRedux<IAppState>) {}
public setBizzes = (bizzes: any) => {
return this.ngRedux.dispatch({
type: BIZZES_SET,
payload: {
bizzes: bizzes
}
});
};
public sortBizzes = (key:string) => {
return this.ngRedux.dispatch({
type: BIZZES_SORT,
payload: {
sortKey: key
}
});
};
}