The below code is facing an issue with deleting the list. Let me know what is the problem I have done. The issue in the case remove section in filter area. Passing id is the problem. Please simplify the issue.
function Reducerthree()
{
const inputRef = useRef();
const initialstate = [{
id: uuid(),
country: "india"
}]
const [items, dispatch] = useReducer((state, action) => {
switch (action.type) {
case'add':
return [
...state,
{
id: uuid(),
country: action.name
}
];
case 'remove':
return state.filter(s => s.id !== action.id);
default:
return state;
}
}, initialstate);
function handleSubmit(event) {
event.preventDefault();
dispatch({
type: 'add',
name: inputRef.current.value
});
inputRef.current.value = '';
}
return (
<div>
<form onSubmit={handleSubmit}>
<input ref={inputRef} />
</form>
<ul>
{items.map((item, index) => (
<li key={item.id}>{item.country}
<button onClick={() => dispatch({type:'remove', index})}>close</button>
</li>
))}
</ul>
</div>
)
}
export default Reducerthree