Created
May 23, 2024 12:43
-
-
Save Gaurav8757/a0f6c5aeab2cfab03d370d8e37aadf9a to your computer and use it in GitHub Desktop.
Not sent all data as props only user click or select data pass to update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
// List component | |
const ListWithUpdate = () => { | |
const [APIData, setAPIData] = useState([]); | |
// STATE TO MANAGE POPUP AND DATA | |
const [showUpdatePopup, setShowUpdatePopup] = useState(false); | |
const [selectedItem, setSelectedItem] = useState(null); | |
// SEND DATA WITH ID AND POPUP TRUE TO OPEN | |
const handleUpdateClick = (id) => { | |
setSelectedItem(id); | |
setShowUpdatePopup(true); | |
}; | |
// CLOSE POPUO AND EMPTY THE DATA | |
const handleClosePopup = () => { | |
setSelectedItem(null); | |
setShowUpdatePopup(false); | |
}; | |
return ( | |
<div> | |
<ul> | |
{APIData.map((item) => ( | |
<li key={item.id}> | |
{item.name} | |
<button onClick={() => handleUpdateClick(item)}>Update</button> | |
</li> | |
))} | |
</ul> | |
// SHOW POPUP AND CONTAINER WITH DATA | |
{showUpdatePopup && ( | |
<div className="popup-overlay"> | |
<div className="popup"> | |
<button onClick={handleClosePopup}>Close</button> | |
// PASS TO UPDATE COMPONENT | |
<UpdateComponent item={selectedItem} onClose={handleClosePopup} /> | |
</div> | |
</div> | |
)} | |
</div> | |
); | |
}; | |
export default ListWithUpdate; | |
// Update component GET DATA AS item, and CLOSE BUTTON PROPS as onClose | |
const UpdateComponent = ({ item, onClose }) => { | |
const [updatedValue, setUpdatedValue] = useState(item.value); | |
const handleInputChange = (e) => { | |
setUpdatedValue(e.target.value); | |
}; | |
const handleUpdate = () => { | |
onClose(); // Close the update popup | |
}; | |
return ( | |
<div className="update-popup"> | |
<h2>Update Item</h2> | |
<input type="text" value={updatedValue} onChange={handleInputChange} /> | |
<button onClick={handleUpdate}>Update</button> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment