In my React app I render an array of items returned from the server, using the database id's as keys.
items.map(item => <Item key={item.id}/>)
This works fine, but now I want to do optimistic updating, so I insert new items into the array before submitting them to the server, and then add the database id when the post operation is done. Now my array briefly contains items with no id's causing errors due to the missing keys.
What to do? I know using the index as key is considered an anti-pattern (https://www.menubar.io/react-keys-index/), so that's out of the question. I also don't want to generate new unique id's for the items because then they will all have two unique id's once they have been saved to the database. I considered using random keys for the unsaved objects, but that seems like a precarious solution.
Is there a best practice solution for this situation? I can't be the first person to encounter this issue.