0

Actually im new to react redux and im buliding a project where i want to update a particular object in a array I trying that but im not getting the desire result

const coinState = {
  coins: [],
};
export const coinReducer = (state = coinState, action) => {
  switch (action.type) {
    case "AFTER_BUY": {
      return { ...state, coins: [...state.coins, action.payload] };
    }
    case "AFTER_SELL":
      return {
        ...state,
        coins: state.coins.filter(
          (c) => c.coinName !== action.payload.coinName
        ),
      };
    case "UPDATE_BUY": {
      const newArr = [...state.coins];
      newArr.map((coin) => {
        if (coin.id === action.payload.id) {
          Here i want to add previous and new state eg : coin.AmountofCoins=10 and
          payload coins is 10 then i want 20 as sum but
          getting 1010 as result
         
          coin.AmountOfCoins += action.payload.AmountOfCoins;

        }
      });
      return {
        coins: newArr,
      };
    }

1 Answers1

-1

It seems that one or both of your amounts are strings instead of integers. Try using

coin.AmountOfCoins += parseInt(action.payload.AmountOfCoins, 10)

to coerce that to an integer.

larz
  • 5,724
  • 2
  • 11
  • 20