我没有得到我想要的结果
Im not getting the result what i want
实际上我是 React redux 的新手,我正在构建一个项目,我想在其中更新数组中的特定对象
我试过了,但我没有得到想要的结果
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,
};
}
您的一个或两个金额似乎是字符串而不是整数。尝试使用
coin.AmountOfCoins += parseInt(action.payload.AmountOfCoins, 10)
将其强制转换为整数。
实际上我是 React redux 的新手,我正在构建一个项目,我想在其中更新数组中的特定对象 我试过了,但我没有得到想要的结果
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,
};
}
您的一个或两个金额似乎是字符串而不是整数。尝试使用
coin.AmountOfCoins += parseInt(action.payload.AmountOfCoins, 10)
将其强制转换为整数。