亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 usereducer hooks 在過濾器方法中傳遞 id 時出錯

使用 usereducer hooks 在過濾器方法中傳遞 id 時出錯

米琪卡哇伊 2023-10-14 18:39:54
下面的代碼面臨刪除列表的問題。讓我知道我做了什么問題。該案例中的問題是刪除過濾區域中的部分。傳遞 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
查看完整描述

1 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

你需要通過id行動,而不是通過它。將刪除操作更改為:

 <button onClick={() => dispatch({type:'remove', id: item.id})}>close</button>


查看完整回答
反對 回復 2023-10-14
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號