I have a table and edit/delete button on that table(each row) to edit/delete corresponding row.
I want to open a popup when the edit is clicked but I want to open the popup with some parameters to show like “old value, new value” etc.
Here is my code for table and I put an EditUserPopup component at bottom.
JavaScript
x
53
53
1
function MainPanel(props) {
2
3
const [isEditPopupOpen, setEditPopup] = useState(true);
4
5
6
const deleteCustomer = async (id) => {
7
await service.deleteCustomerById(id);
8
props.refreshTableParam();
9
}
10
11
const editCustomer = async (id, name, surname) => {
12
setEditPopup(true);
13
//WHAT I NEED HERE ?
14
props.refreshTableParam();
15
16
17
}
18
19
return (
20
<>
21
<ReactBootStrap.Table striped bordered hover>
22
<thead>
23
<tr>
24
<th>ID</th>
25
<th>First Name</th>
26
<th>Last Name</th>
27
<th>Edit</th>
28
<th>Delete</th>
29
</tr>
30
</thead>
31
<tbody>
32
{props.param &&
33
props.param.map((item) => (
34
<tr key={item.id}>
35
<td>{item.id}</td>
36
<td>{item.firstName}</td>
37
<td>{item.lastName}</td>
38
<td><Button className='editButton' onClick={() => editCustomer(item.id, item.firstName, item.lastName)}><FontAwesomeIcon icon={faUserEdit} /></Button></td>
39
<td><Button className='deleteButton' onClick={() => deleteCustomer(item.id)}><FontAwesomeIcon icon={faTrashRestore} /></Button></td>
40
</tr>
41
))}
42
</tbody>
43
</ReactBootStrap.Table>
44
{
45
//HOW TO OPEN THAT COMPONENT WITH PARAMS
46
isEditPopupOpen && <EditUserPopup someParamHere={null}/>
47
}
48
49
50
</>
51
);
52
}
53
I am calling editCustomer() function by the button on table and I am thinking to make EditPopup somehow visible with some param, and in other component(popup’s itself) I’ll do some logic.
But I cannot reach the id,firstName,lastName values in popup. How can I send corresponding table row values to the popup ?
Advertisement
Answer
You can create a react state and set them inside the edit function. Then you should send them as props to your pop up.
JavaScript
1
55
55
1
function MainPanel(props) {
2
3
const [isEditPopupOpen, setEditPopup] = useState(true);
4
const [customerInfo, setCustomerInfo] = useState({id: '', name: '', surname: ''})
5
6
7
const deleteCustomer = async (id) => {
8
await service.deleteCustomerById(id);
9
props.refreshTableParam();
10
}
11
12
const editCustomer = async (id, name, surname) => {
13
setCustomerInfo({id: id, name: name, surname: surname})
14
setEditPopup(true);
15
//WHAT I NEED HERE ?
16
props.refreshTableParam();
17
18
19
}
20
21
return (
22
<>
23
<ReactBootStrap.Table striped bordered hover>
24
<thead>
25
<tr>
26
<th>ID</th>
27
<th>First Name</th>
28
<th>Last Name</th>
29
<th>Edit</th>
30
<th>Delete</th>
31
</tr>
32
</thead>
33
<tbody>
34
{props.param &&
35
props.param.map((item) => (
36
<tr key={item.id}>
37
<td>{item.id}</td>
38
<td>{item.firstName}</td>
39
<td>{item.lastName}</td>
40
<td><Button className='editButton' onClick={() => editCustomer(item.id, item.firstName, item.lastName)}><FontAwesomeIcon icon={faUserEdit} /></Button></td>
41
<td><Button className='deleteButton' onClick={() => deleteCustomer(item.id)}><FontAwesomeIcon icon={faTrashRestore} /></Button></td>
42
</tr>
43
))}
44
</tbody>
45
</ReactBootStrap.Table>
46
{
47
//HOW TO OPEN THAT COMPONENT WITH PARAMS
48
isEditPopupOpen && <EditUserPopup customerInfo={customerInfo} someParamHere={null}/>
49
}
50
51
52
</>
53
);
54
}
55