I am storing an array in state and looping over it to display content on my website.
This is the function that I am calling to loop over the array stored in state:
displayRefundSearchResults2 = () => {
let refundSearchMatch = this.state.refundSearchMatch
if(refundSearchMatch.length === 0){return}
refundSearchMatch = refundSearchMatch.map(e => {e.userEvent = this.state.myEvent; return e})
console.log('refundSearchMatch', refundSearchMatch.map(e => e._id), refundSearchMatch.map(e => e.checkedIn), '---------')
return (<>{refundSearchMatch.map((ticket, index)=> {
return( <div key={index}>
<PurchasedTicket
ticket = {JSON.parse(JSON.stringify(ticket))}
requestFromOrganiser = {true}
index={index}
/>
</div>)}
)}</>)
}
and this is the child component's code:
const PurchasedTicket = (props) =>{
return(
<div className={'ticket-detail ticket-detail-price'}>
<span>{'Ticket ID'}</span>
<h2>{ticket._id}</h2>
<span>{'Checked In'}</span>
<h2>{String(ticket.checkedIn)}</h2>
<span>{'--------'}</span>
</div>
)
}
export default withRouter(PurchasedTicket);
The data from the console.log matches perfectly with what shows up on screen at first:
refundSearchMatch (4) ['62e6ba84dd5e8206c7b6c969', '62e6ba84dd5e8206c7b6c96c', '62e6ba84dd5e8206c7b6c963', '62e6ba84dd5e8206c7b6c964'] (4) [false, false, false, false] ---------
When I tick the checkbox I have coded so that an additional element is added to the array in state.
Now, the data from the console.log and the data on screen are different:
refundSearchMatch (5) ['62e6ba84dd5e8206c7b6c969', '62e6ba84dd5e8206c7b6c96c', '62e6ba84dd5e8206c7b6c965', '62e6ba84dd5e8206c7b6c963', '62e6ba84dd5e8206c7b6c964'] (5) [false, false, true, false, false] ---------
React appears to be taking a short cut. It sees that there is an extra element in the array and it just displays the final element again. However, the new element was inserted into the middle of the array. This element is not displayed at all and the final element is duplicated instead.
How can I ensure react loops over the full contents of the array the second time?


keys are so important. Please read through reactjs.org/docs/lists-and-keys.html. You'll need to use something besides the index so that React knows which elements are which. It's less of a React "shortcut" and more of a limitation (an understandable one)JSON.parse(JSON.stringify(ticket))would be necessary. It seems like a lot of work to get the same data you already had