react I wish clicking a button would reduce an item, but clicking a button would do nothing ... please help me...
const [cars, setCars] = useCars([])
const handleDelete = id => {
const remaining = cars.filter(car => car._id !== id);
setCars(' ');
}
please check this i have written sample code
import React, { useState } from 'react';
export default function App() {
const [lists, setLists] = useState([]);
const remove = (index) => {
// REMOVING THE RECORD BASED ON THE PREVIOUS STATE VALUE - ON THE GIVEN INDEX
setLists((v) => {
const a = [...v];
a.splice(index, 1);
return [...a];
});
};
// FOR ADDING SAMPLE DATA
const addList = () => {
setLists((v) => [...v, { name: 'List data ' + lists.length }]);
};
return (
<div>
<button onClick={addList}>Add List</button>
<ul>
{lists.map((e, k) => (
<li key={k}>
{e.name} <button onClick={(k) => remove(k)}>Remove</button>{' '}
</li>
))}
</ul>
</div>
);
}