0

hey there I am trying to remove the object and set a new data but however each time I try it calls back the previous state. for example remove for emma return ([ {name:"red"},{name:"reeed"} ], then a remove for reed should return ([ {name:"red"} ] however it returns ([ {name:"red"},{name:"emma"}]. Thanks in advance

const [test1, setTest] = useState([ {name:"red"},{name:"reeed"},{name:"emma"}]);

const deleteFunction= (name) => { 
        setTest([...test1.filter(item => item.name !==  name)]);       
    };

1 Answers1

-1

Solution :

setTest((prevState) => (prevState.filter(item => item.name !== name)));