鑒于以下兩個組件,我希望 EntryList 組件在單擊 EntryForm 中的按鈕后 handleEnttryDelete 中的狀態(tài)發(fā)生變化后重新呈現(xiàn)。當(dāng)前狀態(tài)發(fā)生變化,但 UI 并未自行更新:import React, { useState } from "react";import Button from "@material-ui/core/Button";import { render } from "@testing-library/react";const EntryList = (props) => { const [entryList, setEntryList] = useState(props.data); const handleEntryDelete = (entry) => { const newState = entryList.filter(function (el) { return el._id != entry._id; }); setEntryList(() => newState); }; return ( <div> {entryList.map((entry) => { return ( <EntryForm entry={entry} handleEntryDelete={handleEntryDelete} /> ); })} </div> );};const EntryForm = (props) => { const [entry, setEntry] = useState(props.entry); return ( <div> <Button onClick={() => props.handleEntryDelete(entry)}> {entry._id} </Button> </div> );};export default EntryList;
為什么回調(diào)后組件不重新渲染?
慕村9548890
2022-12-02 15:46:27