[英]Date subscription get triggered when other part of the state change in ngrx
I have a start
and end
property which stores a date in my state
.Anytime other part of the state get changed my subscription
for start
and end
date get triggered.我有一个
start
和end
属性,它在我的state
存储一个日期。任何时候state
其他部分发生更改,我对start
和end
日期的subscription
都会被触发。
this is my subscription这是我的订阅
this.subs.sink = this.store
.select(fromTransactionReducer.selectStartAndEndDate)
.subscribe((date) => {
console.log("range", date);
this.range.setValue({
start: date.start,
end: date.end,
});
});
this is the selector这是选择器
export const selectStartAndEndDate = createSelector(
selectTransactionState,
(state) => ({ start: state.start, end: state.end })
);
this is the dateRange reducer这是 dateRange 减速器
on(transactionActions.UpdateDateRange, (state, { start, end }) => ({
...state,
start,
end,
})),
this is the date range action这是日期范围操作
export const UpdateDateRange = createAction(
"[Transaction Page] Update Date Range",
props<{ start: Date; end: Date }>()
);
this is my state这是我的状态
export interface State {
transaction: Transaction[];
cursors: Cursor;
totalTransactions: number;
loading: boolean;
errorMessage: string;
formErrorMessage: string;
items_per_page: number;
pageSizeOptions: number[];
pageIndex: number;
searchKey: string;
formMessage: string;
start: Date;
end: Date;
trans_type: string;
base_type: string;
}
export const initialState: State = {
transaction: [],
cursors: {
after: "",
before: "",
hasNext: false,
hasPrevious: false,
},
totalTransactions: 0,
loading: false,
errorMessage: null,
formErrorMessage: null,
items_per_page: 5,
pageSizeOptions: [2, 3, 5, 10, 15],
pageIndex: 0,
searchKey: "",
formMessage: "",
start: null,
end: null,
trans_type: null,
base_type: null,
};
Anytime i dispatch a different action
for example this.store.dispatch(transactionActions.ResetPageIndex());
任何时候我调度一个不同的
action
,例如this.store.dispatch(transactionActions.ResetPageIndex());
, my subscription
for the date get triggered. ,我对日期的
subscription
被触发。
why is that ?这是为什么 ?
In my reducer i only update start
and end
date when UpdateDateRange
action is dispatched.在我的减速器中,我只在调度
UpdateDateRange
操作时更新start
和end
日期。
It's by design.这是设计使然。
store$
emits the entire state on every state change store$
在每次状态更改时发出整个状态select
pipes those state changes through your selector to give you only the state you are interested in select
通过您的选择器管道这些状态更改,只为您提供您感兴趣的状态select
uses memoization so that if there are no changes to it's inputs, it returns the last calculated value without calling its projector function. select
使用记忆化,因此如果它的输入没有变化,它会返回最后计算的值而不调用它的投影器函数。 Note that it still emits, even though there was no input value change.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.