Answer the question
In order to leave comments, you need to log in
How to pass a value from one function to another in a functional component?
You need to delete a row from the table, but before deleting show a modal with a warning. How can the modal itself find out the ID of the table row being deleted?
const Component = ({ rows }) => {
const [modalIsVisible, setModal] = false;
const deleteRow = () => {
// how to get id of row to delete here?
}
return (
<div>
<table>
{rows.map(row => (
<tr>
<td>
<div>row of id {row.id}</div>
<button onClick={() => setModal(true)}>Delete row</button>
</td>
</tr>
)}
</table>
<Modal isVisible={modalIsVisible}>
<button onClick={deleteRow}>Delete anyway</button>
<button onClick={() => setModal(false)}>Close</button>
</Modal>
</div>
)
}
Answer the question
In order to leave comments, you need to log in
The ID of the deleted line is purely system information that does not affect the display in any way
const [ delId, setDelId ] = useState(null);
<button onClick={() => setDelId(row.id)}>Delete
<Modal isVisible={!!delId}>
<button onClick={() => setDelId(null)}>Close
The useRef() Hook isn't just for DOM refs. The “ref” object is a generic container whose current
property is mutable and can hold any value, similar to an instance property on a class.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question