Redux 有什么奇怪的行为?

Redux 有什么奇怪的行为?,redux,Redux,嘿,伙计们,我想知道为什么我的状态在我处理完这个之后变得不确定: 只想将like属性从1减少到1你们看到我的嵌套处理中的问题了吗 案例“与注释不同”: const unLikeIndex=getKeyIndex( state.list[action.index], action.commentId ); 返回updateObject(状态{ isFetching:false, 名单:{ …state.list, [action.index]:updateValue(unLikeIndex,sta

嘿,伙计们,我想知道为什么我的状态在我处理完这个之后变得不确定:

只想将like属性从1减少到1你们看到我的嵌套处理中的问题了吗

案例“与注释不同”:
const unLikeIndex=getKeyIndex(
state.list[action.index],
action.commentId
);
返回updateObject(状态{
isFetching:false,
名单:{
…state.list,
[action.index]:updateValue(unLikeIndex,state.list[action.index]{
[未链接索引]:{
…state.list[action.index][unLikeIndex],
like:state.list[action.index][unLikeIndex].like-1,
},
}),
},
});
导出常量updateObject=(旧对象,新值)=>
赋值({},oldObject,newValues);
导出常量updateValue=(索引、状态、值)=>
state.map((项目,i)=>{
i==索引?值:项;
});
//测试和工作
导出常量getKeyIndex=(数组,keyToFind)=>{
设ret=-1;
array.forEach(({key},index)=>{
如果(key==keyToFind)ret=index;
});
返回ret;
};
处理前/处理后


老实说,我发现您的代码非常难以阅读,而且可能会有比这更多的错误。但是可以肯定的是,您的
updateValue
函数正在将数组映射到
undefined

export const updateValue = (index, state, value) =>
  state.map((item, i) => {
    i === index ? value : item;
  });
问题是您将map回调放在花括号
{}
中,而没有
return
语句
map
用返回值替换每个元素,因此它用
未定义的
替换数组中的每个元素

export const updateValue = (index, state, value) =>
  state.map((item, i) => {
    i === index ? value : item;
  });
您可以添加单词
return

updateValue = (index, state, value) =>
  state.map((item, i) => {
    return i === index ? value : item;
  });
或者不带大括号直接写

updateValue = (index, state, value) =>
  state.map(
    (item, i) => i === index ? value : item
  );

检查你的列表类型非常感谢你发现了这一点