下面的代碼面臨刪除列表的問題。讓我知道我做了什么問題。該案例中的問題是刪除過濾區域中的部分。傳遞 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
使用 usereducer hooks 在過濾器方法中傳遞 id 時出錯
米琪卡哇伊
2023-10-14 18:39:54