PROBLEM STATEMENT
I am trying to modify a array of objects that is stored in redux store. After updating from a component it doesn't re-render the component.
Basically, I take a redux-state which is a array of objects using mapStateToProps. Then, update a object in this array from a react component. I expect when the array is manipulated the component will re-render with updated array. But, unfortunately when I update the object of this array, my component can't detect the changes.
REDUX STATE
const initialState = {
basket: [
{id: 1, name: "", quantity: 1},
{id: 2, name: "", quantity: 1},
{id: 3, name: "", quantity: 1},
],
};
// My Reducers
const foodReducer = (state = initialState, action) => {
.....................
.....................
.....................
}
REACT COMPONENT
Here, increaseItem function just update the quantity of a item.
Note : When increaseItem function called, redux-dev-tools shows the changes.
function Ordered({ basket }) {
// INCREASE FOOD ITEM
const increaseItem = (id) => {
basket.map(food => {
if(food.id === id){
food.quantity++;
}
});
useEffect(() => {
console.log(basket);
}, [JSON.stringify(basket)]);
return (
{basket.length > 0 &&
basket.map((food) => (
<div className="ofood" key={food.id}>
<div className="no">{food.id}</div>
<div className="name">{food.name}</div>
<div className="quantity">
<div className="btn" onClick={() => increaseItem(food.id)}> + </div>
<div>{food.quantity}</div>
</div>
</div>
))}
);
}
const mapStateToProps = (state) => {
return {
basket: state.food.basket,
};
};
export default connect(mapStateToProps, null)(Ordered);
How can I resolve this issue ????