类型'number'不是React TypeScript Reducer上的数组类型

时间:2019-11-05 18:06:34

标签: reactjs typescript

我在找到索引并尝试减小数字值时遇到此错误。我正在使用带有响应的打字稿。

  

类型'number'不是数组类型。 TS2461

这是代码

减速器

import produce from "immer";
import * as types from "../actionTypes/postActionTypes";

export interface postState {
  posts: Array<any>;
}

const initialState: postState = {
  posts: []
};

const postReducer = (state = initialState, action: any): postState =>
  produce(state, draft => {
    switch (action.type) {
       .....
      case types.DISLIKE_POST_SUCCESS:
        console.log(action);
        const newfindKey = state.posts.findIndex(
          x => x.id === action.payload.id
        );
        console.log(newfindKey);
        draft.posts[newfindKey] = [...(draft.posts[newfindKey].likeCounts - 1)];
        return;

  });

export default postReducer;

1 个答案:

答案 0 :(得分:0)

代替散布对象,我只是这样做了。现在,它会减少likeCounts值。

case types.DISLIKE_POST_SUCCESS:
  console.log(action);
  const newfindKey = state.posts.findIndex(
    x => x.id === action.payload.id
  );
  draft.posts[newfindKey].likeCounts = draft.posts[newfindKey].likeCounts - 1;