React-redux 状态数组管理

React-redux state array management

所以我基本上开始学习 Redux 并想创建一个简单的商店应用程序,您可以在其中将手机添加到购物车。我创建了一个状态对象,并在其中创建了一个包含对象的数组,其中包含商店中的项目列表。我想更新 [+] 单击订购的商品数量,但现在不起作用。我已经为此苦苦挣扎了 1 个小时,但仍然看不出问题出在哪里。

Reducer 看起来像这样:

const initialState = {
liked:0,
cart:0,
item: [
    {
        id:1,
        name: 'Iphone 8',
        price: 2000,
        desc: 'The new Iphone 8 available at our store!',
        orderedNum: 0
    },
    {
        id:2,
        name: 'Iphone 6',
        price: 1500,
        desc: 'The new Iphone 6 available at our store!',
        orderedNum: 0
    },
    {
        id:3,
        name: 'Samsung S8',
        price: 2200,
        desc: 'The new Samsung S8 available at our store!',
        orderedNum: 0
    },
    {
        id:4,
        name: 'Xiaomi Mi 6',
        price: 1400,
        desc: 'The new Xiaomi Mi 6 available at our store!',
        orderedNum: 0
    },
    {
        id:5,
        name: 'Pocophone P1',
        price: 2100,
        desc: 'The new Pocophone P1 available at our store!',
        orderedNum: 0
    },
    {
        id:6,
        name: 'Nokia 3310',
        price: 999,
        desc: 'The new Nokia 3310 available at our store!',
        orderedNum: 0
    },
]
}

const reducer = (state = initialState, action) => {
const newState = {...state};

switch(action.type) {
    case 'ADD_NUM':
        return state.item.map((el, index) => {
            if(el.id === action.id ){
                return {
                    ...el,
                    orderedNum: el.orderedNum + 1
                }
            }

            return el;
        })
    default:
        break;
}

return newState;
}

export default reducer;

我尝试过不同的方法,但我相信这可能是在减速器中嵌套的问题。

有人可以建议吗?

你的减速器应该看起来像这样:

export default (state = initialState, action) => {
  switch (action.type) {
    case 'ADD_ITEM':
      return [...state, action.item];
    case 'REMOVE_ITEM':
      return state.filter(({ id }) => id !== action.id);
    default:
      return state;
  }
};

正如评论中所指出的那样,上面的代码适用于数组而非对象的初始状态...如果它是一个对象,这里是如何处理它(使用来自 lodash 的两个辅助方法 mapKeysomit:

export default (state = {}, action) => {
  switch (action.type) {
    case FETCH_ITEMS:
      return { ...state, ...mapKeys(action.payload, 'id') };
    case FETCH_ITEM:
      return { ...state, [action.payload.id]: action.payload };
    case CREATE_ITEM:
      return { ...state, [action.payload.id]: action.payload };
    case EDIT_ITEM:
      return { ...state, [action.payload.id]: action.payload };
    case DELETE_ITEM:
      return omit(state, action.payload);
    default:
      return state;
  }
};

让我们从你的减速器开始

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case "ADD_NUM":
      return {
        // destruct and return a new object otherwise react wont update the UI
        ...state,
        item: state.item.map(el =>
          el.id === action.id
            ? { ...el , orderedNum: el.orderedNum + action.value }
            : el
        )
      };
    default:
      return state;
  }
};

mapDispatchToProps

const mapDispatchToProps = dispatch => {
  return {
    // add id to addNum
    addNum: id =>
      dispatch({
        type: "ADD_NUM",
        id,
        value: 1
      })
  };
};

项目组件

const Items = ({ item, addNum }) => (
  item.map(el => (
    <div key={el.id}>
      <h1>{el.name}</h1>
      <h3>{el.price}</h3>
      <h3>{`orderedNum: ${el.orderedNum}`}</h3>
      // add the id to addNum
      <button onClick={() => addNum(el.id)}>+</button>
    </div>
  ))
);

CodeSandBox