我有一個按鈕,當您單擊它時應該從列表中刪除一個框,但它不起作用。誰能明白為什么?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} /> )});[...]
onClick 函數不會刪除數組中的項目
PIPIONE
2023-04-27 16:39:54