简体   繁体   English

Redux 过滤数据的最佳实践

[英]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.在开发应用程序的过程中,我面临一个关于我是否正确使用 Redux 的问题。 I have a fav:[] in which I add product objects and render their list.我有一个 fav:[] 在其中添加产品对象并呈现它们的列表。 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:但是,为了不丢失数据,我必须将此 fav:[] 复制到 favCopy:[] ,然后才执行 .filter 示例代码:

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.我们建议大多数时候不要直接在 reducer 中进行过滤。 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 https://redux.js.org/usage/deriving-data-selectors

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

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