In a client project I use NGRX/Store and NGRX/Entity.
While the majority of the store consists of the Entities, I have to store additional values in the state. For business reasons, I need the length of all items at a certain point in time.
export interface State extends EntityState<Item> {
initialItemListSize: number; // this should hold the length of entity-adapters items-list-size at a certain point
}
Anyway, at some point I just want to
this.store.dispactch(saveItemListSizeNow);
call.
Now I'm wondering where I have to implement the logic (get the list length).
At first I thought in the reducer
on(Itemctions.saveItemListSizeNow, (state) => {
const size = ... //<--- no Idea how to get the length here
return { ...state, initialItemListSize: size };
}),
Can someone give me an answer?
You can access the entities on the state.
state.ids.length
Eg:
on(Itemctions.saveItemListSizeNow, (state) => {
const size = ... //<--- no Idea how to get the length here
return { ...state, initialItemListSize: state.ids.length};
}),
Your actions can be defined with payloads data that can be passed on dispatching it, example:
import { createAction, props } from '@ngrx/store';
export const saveItemListSizeNow= createAction(
'[Action Source] Save Item list size',
props<{ itemListSize: number }>()
);
And when you dispatch it,
store.dispatch(saveItemListSizeNow({ itemListSize: givenSize }));
And your reducer would be like this
on(Itemctions.saveItemListSizeNow, (state, action) => {
const size = action.itemListSize;
return { ...state, initialItemListSize: size };
}),
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.