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

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

onClick 函數不會刪除數組中的項目

onClick 函數不會刪除數組中的項目

PIPIONE 2023-04-27 16:39:54
我有一個按鈕,當您單擊它時應該從列表中刪除一個框,但它不起作用。誰能明白為什么?const Box = (props) => {    return (        <>            <div id={props.id} style={{height:`${props.height}em`, width:`${props.width}em`, backgroundColor: props.color }}>             </div>           <Button onClick={props.removeItem}/>        </>    );};export default Box;const BoxList = () => {const [boxes, setBoxes] = useState([{height: "", width:"", color:"", id:""}]);const removeBox = (boxId) => {   const updatedBoxList = boxes.filter(box => box.id !== boxId);    setBoxes(updatedBoxList);  // this is where the update should happen};const boxesArray = boxes.map((box) => {    return(    <Box width={box.height}         height={box.width}         color={box.color}         id={box.id}         removeItem={removeBox}    />    )});[...]
查看完整描述

2 回答

?
慕碼人8056858

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

const Box = (props) => {

    const removeItem = () => {

        props.removeItem(props.id);

    };

    return (

        <>

            <div id={props.id} style={{height:`${props.height}em`, width:`${props.width}em`, backgroundColor: props.color }}>

             </div>

           <Button onClick={removeItem}/>

        </>


    );

};

export default Box;

我已經重新定義了您的 Box 組件,以便它使用props.removeItem函數期望的參數調用函數


查看完整回答
反對 回復 2023-04-27
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

您需要將 boxId 傳遞給 removeItem?,F在它以點擊事件作為參數被調用。



查看完整回答
反對 回復 2023-04-27
  • 2 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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