简体   繁体   English

将之前的状态添加到 NgRX 中的当前状态(列表)

[英]Add previous state to the current state(list) in NgRX

I've been trying to resolve this part.我一直在努力解决这部分。

export const personReducer = createReducer(
    initialState,

    on(LOAD_ALL_SUCCESS, (state, { list }) => {
        // list.push(state.entities)
        const tmpArr : Person[] = Object.values(state.entities)
        
        tmpArr.forEach(t =>{
            list.push(t)
        })

        return adapter.addAll(list, {
            ...state,
            loaded: true,
            loading: false,
            saved201: false,
            updated200: false,
            error: undefined,
            formState: undefined,
        })
    }),
)

My Goal here is to add the previous state to my current state so that my list of person contains the previous and the current which i will display in my ui.我的目标是将以前的状态添加到我的当前状态,以便我的人员列表包含我将在我的 ui 中显示的前一个和当前状态。 Which means i have to display all even if i move to the next page.这意味着即使我移动到下一页,我也必须显示所有内容。

I thought i could convert the state.entities from dictionary to array but it's giving me an error.我以为我可以将state.entities从字典转换为数组,但它给了我一个错误。 Is there a way to do achieve my goal?有没有办法实现我的目标?

Btw.顺便提一句。 in my UI i'm not using the typical pagination but rather scroll down (which display or pull data if the scroll reach the bottom like in facebook news feed)在我的用户界面中,我没有使用典型的分页,而是向下滚动(如果滚动到达底部,就像在 Facebook 新闻提要中那样显示或拉取数据)

I'm using ngrx v10 https://ngrx.io/我正在使用 ngrx v10 https://ngrx.io/

    "@ngrx/effects": "^10.0.0",
    "@ngrx/entity": "^10.0.0",
    "@ngrx/router-store": "^10.0.0",
    "@ngrx/store": "^10.0.0",
    "@ngrx/store-devtools": "^10.0.0",
    "@rxweb/reactive-form-validators": "^2.0.0",

for the selector对于选择器

export const selectAll = createSelector(
    selectorPersonFeature,
    adapter.getSelectors().selectAll
)

So I ended up with this.所以我结束了这个。

this.store.pipe(select(selectAll))
      .subscribe((p: Person[]) => {
        p.forEach(i => {
          this.person$.push(i)
        })
      })

Thanks,谢谢,

there are some other methods you can use...您还可以使用其他一些方法...
try to use adapter.addMany()尝试使用adapter.addMany()
https://ngrx.io/api/data/EntityCollectionReducerMethods#addMany https://ngrx.io/api/data/EntityCollectionReducerMethods#addMany

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

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