Javascript 反应还原剂中的多重作用

Javascript 反应还原剂中的多重作用,javascript,reactjs,redux,reducers,Javascript,Reactjs,Redux,Reducers,我有两个动作来处理我的减速器中的状态 INCREMENT_LIKES VISUALIZE_WIDTH 这些动作单独进行时效果很好,但由于我想将它们配对,所以出现了一些问题 控制台错误 未捕获的TypeError:无法读取未定义的属性“w” 行动 // increment export function visualize(index) { return { type: 'VISUALIZE_WIDTH', index } } export function in

我有两个动作来处理我的减速器中的状态

INCREMENT_LIKES

VISUALIZE_WIDTH
这些动作单独进行时效果很好,但由于我想将它们配对,所以出现了一些问题

控制台错误

未捕获的TypeError:无法读取未定义的属性“w”

行动

// increment
export function visualize(index) {
  return {
    type: 'VISUALIZE_WIDTH',
    index
  }
}

export function increment(index) {
  return {
    type: 'INCREMENT_LIKES',
    index
  }
}
减速器

export default function svgs(state = [], action) {
  switch(action.type) {
    case 'INCREMENT_LIKES' :
      const i = action.index;
      return [
        ...state.slice(0,i), // before the one we are updating
        {...state[i], likes: state[i].likes + 1},
        ...state.slice(i + 1), // after the one we are updating
      ];
    case 'VISUALIZE_WIDTH':
      console.log("Incrementing dats!!");
      return [
        ...state.slice(0,i), // before the one we are updating
        {...state[i], w: state[i].w + 100},
        ...state.slice(i + 100), // after the one we are updating
      ];
    default:
      return state;
  }
  return state;
}
在我的组件中

    <button onClick={this.props.increment.bind(null, i)} className="likes">&hearts; {svg.likes}</button>
    <button onClick={this.props.visualize.bind(null, i)}>Change Data</button>
&hearts;{svg.likes}
更改数据

我的操作有什么错?

可能是初始化错误?SVG的初始状态是什么?您是否检查了状态[i]中的值?尝试验证状态[i].w是否为空。是i=调试时未定义,请查看问题中的图像不确定问题是否存在于您的操作或还原程序中。是否可能没有从组件对象传递状态?你试过从bind语句中删除“null”吗?非常愚蠢的错误我已经纠正了,我现在回答自己