I don't think you need something like redux for this.
To get around your problem, I would do the following:
In the Main Component, update the album constant to const [albums, setAlbums] = useState(data.data)
;
Create a function in the Main Component:
const handleDelete = id => {
setAlbums(albums => albums.filter(album => album._id != id));
}
- Note that as now albums is a state variable, creating a new array with filter will cause the component to re-render when handleDelete is called.
In your Card Component, accept a new prop called onDelete
, and pass the new function handleDelete
from your Main Component into it like so:
<CardWithEdit
width={"23rem"}
height="16rem"
color={album.color}
bckImg={album.bckImgUrl}
key={album._id}
link={`/albums/${album._id}`}
editLink={`edit/${album._id}`}
id={album._id}
onDelete={handleDelete}
>
and:
const CardWithEdit = ({
width,
height,
bckImg,
color,
children,
link,
editLink,
id,
onDelete,
}) => {
In the handleDelete function of your Card Component, after doing the DELETE request, simply call onDelete(id)
And just like that, you should have the functionality that you're requesting. Let me know if there are any issues!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…