简体   繁体   English

redux - reducer - 更改数组中 object 的属性

[英]redux - reducer — change property of an object within an array

i need change property of an object within an array that is part of another object in me reducer, I'm going to show you that I tried and I was unsuccessful but the truth is that I don't know if it is possible to change that property我需要在一个数组中更改 object 的属性,该数组是我减速器中另一个 object 的一部分,我将向您展示我尝试过但没有成功,但事实是我不知道是否可以更改那个属性

function function

 function handleQuantity(e) {
    if (e.target.name === "resta" && quantityProduct > 0) {
      setQuantityProduct(quantityProduct - 1);
    }
    if (e.target.name === "suma" && quantityProduct < props.stock) {
      setQuantityProduct(quantityProduct + 1);
    }
    const actionquantity = {
      orderlineId: props.id,
      orderlineQuantity: quantityProduct,
    };
    props.changequantity(actionquantity);
  }

executor执行人

  function handleChangeQuantity(actionquantity){
      dispatch(changeQuantityProductLine(actionquantity , id))
    }

action行动

export const changeQuantityProductLine = (actionquantity, userId) => async(dispatch) =>{
    try{
        dispatch({type: CHANGE_QUANTITY_PRODUCT_LINE})

        const {data} = await axios.put(`${url}users/${userId}/cart`, actionquantity);
        console.log('-----------------------------------------------------------------------------------------------------------------------------------',actionquantity)
        dispatch({
            type: CHANGE_QUANTITY_PRODUCT_LINE_SUCCESS,
            payload: actionquantity
        })
    }catch(error){
        dispatch({
            type: CHANGE_QUANTITY_PRODUCT_LINE_ERROR,
            payload:
            error.response && error.response.data.message
            ? error.response.data.message
            : error.message,
        })
    }
}

reducer减速器


export const orderlineByIdReducer = (
  state = {
    //cart: []
    order: {},
    error: null,
    loading: false,
  }, 
  action
) => {
  switch (action.type) {
    case ORDER_LINE_BY_ID:
      return { ...state, loading: true};
    case ORDER_LINE_BY_ID_SUCCESS:
      return {...state, loading: false, order: action.payload };
    case ORDER_LINE_BY_ID_ERROR:
      return {...state, loading: false, error: action.payload };
    case REMOVE_ORDER_LINE_BY_ID:
      return { ...state, loading: true };
    case REMOVE_ORDER_LINE_SUCCESS:
      return {
        ...state,
        order: {...state.order, orderlines: state.order.orderlines.filter(
          (orderline) => orderline.product.id != action.payload
        ), }
      };
    case REMOVE_ORDER_LINE_ERROR:
      return {...state, loading: false, error: action.payload };
      case CHANGE_QUANTITY_PRODUCT_LINE:
        return { ...state, loading: true };
      case CHANGE_QUANTITY_PRODUCT_LINE_SUCCESS:
        return {
          ...state,
           order: {...state.order, orderlines: state.order.orderlines.map(
             orderline => orderline.id === action.payload.actionquantity.orderlineId
              ? Object.assign({}, orderline, {quantity: action.payload.actionquantity.orderlineQuantity})
              : orderline) }
          }
      case CHANGE_QUANTITY_PRODUCT_LINE_ERROR:
        return {...state, loading: false, error: action.payload };

    default:
      return state;
  }
};

With that attempt the action breaks, but taking out what I put in the reducer works fine... I suspect that I am not managing to change the property of that orderline通过这种尝试,动作中断,但是取出我放入减速器的东西效果很好......我怀疑我没有设法改变该订单线的属性

It doesn't look like your payload has actionquantity as a property.看起来您的有效负载没有actionquantity作为属性。 The payload is the actionquantity.有效负载是操作量。 So, action.payload.actionquantity.orderlineId should be action.payload.orderlineId .所以, action.payload.actionquantity.orderlineId应该是action.payload.orderlineId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM