简体   繁体   English

当 ngrx 中的其他部分状态更改时触发日期订阅

[英]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.我有一个startend属性,它在我的state存储一个日期。任何时候state其他部分发生更改,我对startend日期的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操作时更新startend日期。

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.请注意,即使没有输入值更改,它仍然会发出。
  • if you are using say Angular, it's change detection accommodates this behaviour by not firing when the selector value is the same.如果您使用的是 Angular,它的更改检测通过在选择器值相同时不触发来适应这种行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM