如何点击按钮会显示一个item
How to click on the button will show an item
反应
我希望点击一个按钮可以减少一个项目,但是点击一个按钮什么也做不了……请帮帮我……
const [cars, setCars] = useCars([])
const handleDelete = id => {
const remaining = cars.filter(car => car._id !== id);
setCars(' ');
}
请检查我已经写了示例代码
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>
);
}
反应 我希望点击一个按钮可以减少一个项目,但是点击一个按钮什么也做不了……请帮帮我……
const [cars, setCars] = useCars([])
const handleDelete = id => {
const remaining = cars.filter(car => car._id !== id);
setCars(' ');
}
请检查我已经写了示例代码
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>
);
}