So I want to update a specific value inside an array of objects, but it's not updating correctly
P粉842215006
2023-08-18 13:23:19
<p>So I try to copy the array and when it matches the memberId I update the selected value of the specific object.
For a single member it is valid.
However, assuming there are multiple members, it doesn't update correctly. </p>
<pre class="brush:php;toolbar:false;">const updateGroupMembers = (memberId: string, optedRider: string, value: string) => {
const updatedMembers = group?.memberDetails?.map((member) =>
member.memberId === memberId
? { ...member, [optedRider]: value }
: member
);
if (updatedMembers) {
setGroup((prevGroup) => ({
...prevGroup!,
memberDetails: updatedMembers,
}));
}
};</pre>
<p><br /></p>
You can do this:
useEffect will run once
updatedMembers
is updated.