下面的代码面临删除列表的问题。让我知道我做了什么问题。该案例中的问题是删除过滤区域中的部分。传递 id 是问题。请简化问题。
函数Reducer Three()
{
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
慕丝7291255
相关分类