简体   繁体   中英

Redux best practice to filter data

in the process of developing an application, I'm facing a question about whether I'm using Redux correctly. I have a fav:[] in which I add product objects and render their list. However, in order for the data not to be lost, I have to copy this fav:[] to favCopy:[] and only after that execute .filter Example code:

case "fav":
                state.fav = action.payload.filter === 'all'
                    ? state.favCopy
                    : state.favCopy.filter((item: any) => item[type] === action.payload.filter)

                break;

I would like to understand how right I am by keeping the original array intact? Maybe there is a way not to multiply arrays and use only one state?

We would recommend not doing filtering directly in the reducer most of the time. Instead, keep the original array in state as-is, and then also store a description of how you want the filtering to be done. From there, use selector functions to derive the filtered value as needed:

https://redux.js.org/usage/deriving-data-selectors

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.

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