Reputation: 325
I'm new to React,currently im working on React Redux application and I want to remove item inside array in Redux State .After I removing the one item(mobile) List shows as undefined in UseSelector
const mobileList = useSelector((state) => state.MobileReducer.mobileList);
and due to that map is not working since list is undefined but after i got the error i checked the state from Redux dev tools and it shows array inside the list without the removed item and this code is working fine when adding item , state updates and list is also showing with the latest .
List Component
import React, { useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import Table from "react-bootstrap/Table";
import Button from "react-bootstrap/Button";
import allActions from "../Actions/index";
import EditModal from "./EditModal";
const MobileList = () => {
const dispatch = useDispatch();
debugger;
const mobileList = useSelector((state) => state.MobileReducer.mobileList);
const [modalShow, setModalShow] = useState(false);
const editingItem = useSelector((state) => state.editingItem);
const [editItem, setEditItem] = useState(false);
useEffect(() => {
if(editItem){
setModalShow(true);
}else{
setModalShow(false);
}
}, [editItem]);
return (
<>
<h1>Available Mobiles</h1>
<Table responsive>
<thead>
<tr>
<th>Model Name</th>
<th>Brand Name</th>
<th>Year</th>
<th>Price</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{mobileList.map((item, i) => {
return [
<tr key={i}>
<td>{item.ModelName}</td>
<td>{item.Brand}</td>
<td>{item.Year}</td>
<td>{item.Price}</td>
<td>
<Button variant="info" onClick={() => setEditItem(item)}>
Edit
</Button>
</td>
<td>
<Button
variant="danger"
onClick={() =>
dispatch(allActions.MobileActions.deleteItem(item))
}
>
Delete
</Button>{" "}
</td>
</tr>,
];
})}
</tbody>
</Table>
{modalShow ? (
<EditModal
show={modalShow}
onHide={() => setModalShow(false)}
item={editItem}
onClean={() => setEditItem(null)}
/>
) : null}
</>
);
};
export default MobileList;
Reducer
const initialMobileListState = {
mobileList:[],
editingItem:null
}
const counter = (state = initialMobileListState, action) => {
debugger;
switch(action.type){
case "SAVE":
return {
...state,
mobileList:[...state.mobileList, action.mobile]
}
case "EDIT":
return {
...state,
editingItem:[action.mobile]
}
case "DELETE":
return state.mobileList.filter(a=>a.Id !== action.mobile.Id)
default:
return state
}
}
export default counter
Upvotes: 0
Views: 337
Reputation: 2849
You need to fix Reducer like this. When deleting an item, you need to keep state
in the original object structure, as you did when saving an item.
const initialMobileListState = {
mobileList:[],
editingItem:null
}
const counter = (state = initialMobileListState, action) => {
debugger;
switch(action.type){
case "SAVE":
return {
...state,
mobileList:[...state.mobileList, action.mobile]
}
case "EDIT":
return {
...state,
editingItem:[action.mobile]
}
case "DELETE": // <--------------------------------
return {
...state,
mobileList: state.mobileList.filter(a=>a.Id !== action.mobile.Id)
}
default:
return state
}
}
export default counter
Upvotes: 1