So I want to update a specific value inside an array of objects, but it's not updating correctly
P粉842215006
P粉842215006 2023-08-18 13:23:19
0
1
446
<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>
P粉842215006
P粉842215006

reply all(1)
P粉957723124

You can do this:

useEffect(() => {
    setGroup((prevGroup) => ({
      ...prevGroup!,
      memberDetails: updatedMembers,
  }));
  }, [updatedMembers])

useEffect will run once updatedMembers is updated.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!