Javascript 在redux上的多个位置更新值时出现问题
我需要你的帮助来更新redux上的一些数据,我有一个产品服务商店,它由产品和产品组组成,我设法更新产品,但我还需要更新我的组中包含的产品(在组对象->产品:[{…},{…}])。问题是,我的函数返回null,而不是我的两个包含更新产品的组。。。。我真的不明白为什么我保持着与更新一个简单产品相同的逻辑 提前感谢那些有勇气阅读的人:)Javascript 在redux上的多个位置更新值时出现问题,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我需要你的帮助来更新redux上的一些数据,我有一个产品服务商店,它由产品和产品组组成,我设法更新产品,但我还需要更新我的组中包含的产品(在组对象->产品:[{…},{…}])。问题是,我的函数返回null,而不是我的两个包含更新产品的组。。。。我真的不明白为什么我保持着与更新一个简单产品相同的逻辑 提前感谢那些有勇气阅读的人:) 案例“更新产品”: 返回{ products\u services:state.products\u services.map(项目=>{ //如果项目知道产品标签,
案例“更新产品”:
返回{
products\u services:state.products\u services.map(项目=>{
//如果项目知道产品标签,则为产品
if(项目、产品和标签){
//如果其id对应于更新的产品
if(item.id==action.payload.id){
//替换旧值
返回Object.assign(项目、操作、有效负载)
//sinon retourne l'item
}否则{
退货项目
}
//如果项目是一个组,则您将检查其产品
}else if(项目组名称){
item.Products.map((产品)=>{
//如果在其产品中有一个包含id,则替换它
if(product.id==action.payload.id){
返回Object.assign(产品、动作、有效载荷);
//否则,您将退回旧产品
}否则{
退货产品;
}
});
}
}),
}
在redux商店中更新产品之前:
在redux商店中更新产品后:
在
中,如果
阻塞,则粗略返回item.Products.map((product)
)的结果:
return {
products_services: state.products_services.flatMap((item) => {
// if the item knows product_label it is a product
if (item.product_label) {
// if its id corresponds to the updated product
if (item.id === action.payload.id) {
// replace the old values
return [Object.assign(item, action.payload)];
//sinon retourne l'item
} else {
return [item];
}
// if the item is a group then you will check its products
} else if (item.group_name) {
item.Products.map((product) => {
// if in its products one contains the id then you replace it
if (product.id === action.payload.id) {
return Object.assign(product, action.payload);
// otherwise you return the old product
} else {
return product;
}
});
}
}),
};
最后,没有任何类型的else
块,因此可能两个条件都不满足。添加某种类型的else/fallback逻辑可能会更好
希望这会有所帮助!尝试更新到
返回item.Products.map((product)=>{
因为您当前没有在中返回映射值,否则如果块。您让我走上正轨了,谢谢:)
return {
products_services: state.products_services.flatMap((item) => {
// if the item knows product_label it is a product
if (item.product_label) {
// if its id corresponds to the updated product
if (item.id === action.payload.id) {
// replace the old values
return [Object.assign(item, action.payload)];
//sinon retourne l'item
} else {
return [item];
}
// if the item is a group then you will check its products
} else if (item.group_name) {
item.Products.map((product) => {
// if in its products one contains the id then you replace it
if (product.id === action.payload.id) {
return Object.assign(product, action.payload);
// otherwise you return the old product
} else {
return product;
}
});
}
}),
};