redux 减速器中的 forEach 是反模式吗?
我有一个 forEach 循环,它检查传入数据的某个属性是否包含“/”,如果它确实将“/”替换为“-”,我想知道这是否是有效的 redux 代码,或者我正在创建某种反-模式代码就在这里。
case actions.GET_PRODUCTS: {
const data = { ...payload };
data.forEach(item => {
item.options.forEach(option => {
if (option.includes('/')) {
option.cleanVersion = option.replace('/', '-');
}
});
});
return {
...state,
items: data,
};
}
或者我可以使用类似于 normalizr 的东西来处理这个问题吗?如果是这样,我们将不胜感激
I have a forEach loop which checks if a certain attribute of data coming in includes '/' if it does im replace the '/' with '-', i was wondering if that is valid redux code or im creating some sort of anti-pattern code right here.
case actions.GET_PRODUCTS: {
const data = { ...payload };
data.forEach(item => {
item.options.forEach(option => {
if (option.includes('/')) {
option.cleanVersion = option.replace('/', '-');
}
});
});
return {
...state,
items: data,
};
}
Or could i use something along the lines of normalizr to handle this? and if so an example would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
减速器中的 forEach 完全没问题。
不过,您应该意识到,您正在编写一种总体上已经过时多年的 Redux 风格。现代 Redux 不使用 switch..case 减速器、ACTION_TYPEs、不可变减速器逻辑、createStore 或 connect。
因此,它的代码量只有 1/4,并且更不容易出错。
您可以按照官方 Redux 教程。
A forEach in a reducer is perfectly fine.
You should be aware though that you are writing a style of Redux in genral that is outdated by years. Modern Redux does not use switch..case reducers, ACTION_TYPEs, immutable reducer logic, createStore or connect.
As a result, it's 1/4 of the code and much less prone to errors.
You can read up on modern Redux by following the official Redux tutorial.