//最高和最低楼层输入框组件
function FloorInterval({ handleSave }) {
const [priceItems, setPriceItems] = useState([{ base_price: '' }]);
const addMore = () => {
const a = [...priceItems];
a.push({ base_price: '' });
setPriceItems(a);
};
function subtraction(index) {
const a = [...priceItems];
const newPriceItems = a.filter((_, i) => i !== index);
setPriceItems(newPriceItems);
handleSave(newPriceItems);
}
function handleChange(index, type, value) {
const a = [...priceItems];
a[index][type] = value;
setPriceItems(a);
handleSave(priceItems);
}
return (
<div className="append-container">
{priceItems.map(({ base_price, min_floor, max_floor }, index) => (
<div className="append-last" key={index}>
<div style={{ display: 'flex' }}>
<Form.Item label="市场价" style={{ marginBottom: 0 }}>
<Input
style={{ width: '184px' }}
defaultValue={base_price}
name="priceList[index].base_price"
placeholder="市场价"
onChange={(e) =>
handleChange(index, 'base_price', Number(e.target.value))
}
/>
</Form.Item>
<span> </span>
<Form.Item label="楼层区间" style={{ marginBottom: 0 }}>
<Input
defaultValue={min_floor}
placeholder="最小值"
name="min_floor"
style={{ width: '65px' }}
onChange={(e) =>
handleChange(index, 'min_floor', Number(e.target.value))
}
/>
<span>--</span>
<Input
defaultValue={max_floor}
placeholder="最大值"
name="max_floor"
style={{ width: '65px' }}
onChange={(e) =>
handleChange(index, 'max_floor', Number(e.target.value))
}
/>
</Form.Item>
<span> </span>
{index === 0 ? (
<div>
<Button
type="primary"
shape="circle"
size="small"
onClick={addMore}
> + </Button>
</div>
) : (
<div>
<Button
type="danger"
shape="circle"
size="small"
onClick={() => subtraction(index)}
> -</Button>
</div>
)}
</div>
</div>
))}
</div>
);
}
组件对应的内容就是

现在的问题:
点击第二行的减号按钮之后,列表中删掉的是index为1 的数据,但是页面中删除的永远是最后一行?

