Working with React.js is really enjoyable.
I created a simple commentary application based on the official tutorial.
You can add, edit and delete comments. They are pulled through the GET every 10 seconds.
At some point, optimistic updates are mentioned in the manual: updating the user interface before the server responds in case of a create, update or delete operation.
Because comments are part of the list, React suggests assigning a unique key to each comment.
Therefore, I use the database identifier of each comment as a key. This works great for update and delete operations.
However, in the case of the create operation, I do not know the comment database identifier until it was actually created on the server side, and therefore I do not know what value to assign to the key.
At this point, the comment is added to the comment list but does not have a key / identifier and therefore cannot be edited or deleted until the list is updated during the next API poll.
Can I get around this?
source share