I am trying to implement a delete button to remove a row from the table inside a modal using Ant Design UI library. However, whenever I call the remove function, even if I have previously added several new items on top of the initial items, the data
state usage inside the removeItem
component is not updated and always uses the initial version. Whenever I output the data
state from inside the removeItem
component, it always shows the 3 items from the beginning.
But the weird thing is if I output the data
content from inside the addItem
component or useEffect
hook, it shows the added new rows. I cannot figure out where the issue lies.
Edit: link to codesandbox (edit barang -> add some new items -> remove new item -> see console log) https://codesandbox.io/embed/wispy-meadow-czv6p?file=/src/App.js&codemirror=1
import { useState, useEffect } from "react";
const ModalBarang = (props) => {
const dataSource = [
{
key: 1,
code: "A123",
name: "Barang 1",
desc: "Description 1",
qty: 30,
price: 10000,
total: 30 * 10000,
},
{
key: 2,
code: "B123",
name: "Barang 2",
desc: "Description 2",
qty: 10,
price: 20000,
total: 10 * 20000,
},
{
key: 3,
code: "B123",
name: "Barang 2",
desc: "Description 2",
qty: 10,
price: 20000,
total: 10 * 20000,
},
];
const tableColumns = [
{
title: "No",
dataIndex: "key",
},
{
title: "Kode Barang",
dataIndex: "code",
render: (text, _) => {
return <Input value={text} />;
},
},
{
title: "Nama Barang",
dataIndex: "name",
render: (text, _) => {
return <Input value={text} />;
},
},
{
title: "Deskripsi",
dataIndex: "desc",
render: (text, _) => {
return <Input value={text} />;
},
},
{
title: "Qty",
dataIndex: "qty",
render: (text, _) => {
return <Input value={text} />;
},
},
{
title: "Harga",
dataIndex: "price",
render: (text, _) => {
return <Input value={text} />;
},
},
{
title: "Total",
dataIndex: "total",
},
{
title: "Hapus",
dataIndex: "remove",
render: (_, record) => {
return (
<Button
type="default"
danger="true"
onClick={() => removeItem(record.key)}
>
Hapus Barang
</Button>
);
},
},
];
const [columns, setColumns] = useState(tableColumns);
const [data, setData] = useState(dataSource);
const addItem = () => {
const newRow = {
key: data.length + 1,
code: "",
name: "",
desc: "",
qty: 0,
price: 0,
total: 0,
};
const newData = [...data, newRow];
setData(newData);
};
const removeItem = (key) => {
console.log(data); // ----> even after adding several items (addItem) the log only shows the initial values just like in datasource. E.g. initial items = 3, addItems 2 times so there should be 5 items. But when trying to remove item using this component, this console log only outputs the 3 initial items.
const newItems = data.filter((item) => item.key !== key);
setData(newItems);
};
return (
<Modal
title="Barang"
visible={props.visible}
onOk={props.onOk}
onCancel={props.onCancel}
>
<Table dataSource={data} columns={columns} pagination={{ pageSize: 5 }} />
<Button type="primary" htmlType="button" onClick={addItem}>
Tambah Barang
</Button>
</Modal>
);
};
export default ModalBarang;