In a React app I have a component that lists people invited to a project. When I remove an item from the array that's used as the source for the list, React isn't updating the list on the page.
The list of invites are passed to the component as props. There is a link on each invite to cancel the invitation, to this event I have attached a function that removes that invite from the array, but for some reason it is not being removed from the list shown on the page.
This list of invites is created inside the component via a function I've created;
function invites(teamInvites) {
if (!teamInvites) {
return null;
}
return (
<ul className={styles.pendingInvites}>
{teamInvites.map((invite, index) => (
<li key={invite.id} id={invite.id}>
<span class={styles.email}>{invite.email}
<span className={styles.pending}>Pending</span></span>
<span class={styles.right}>
<a onClick={() => {actionResendTeamUserInvite(teamId, teamName, invite.email)}}>Resend Invite</a> |
<a onClick={() => {removeInvite(invite.email)}}>Cancel</a>
</span>
</li>
))}
</ul>
);
}
This is called in the component like this;
{invites(teamInvites)}I then have a function which removes the invite from the teamInvites array;
const removeInvite = (email) => {
actionRemoveTeamUserInvite(teamId, email);
let index = 0;
let removeAtIndex = -1;
teamInvites.forEach(invite => {
if (invite.email === email) {
removeAtIndex = index;
}
index++;
});
if (removeAtIndex >= 0) {
removeAtIndex = -1;
}
return false;
}
I have confirmed that the array itself is being updated by outputting the length of the array, which reduces by one each time the function above is executed.
Why does the view itself not update along with the array?
actionRemoveTeamUserInvite? Are you using redux?