We are creating an React application, which has lists with basic CRUD operations in multiple places. The basic example, of course is:
- Fill input to have name for the item, click submit
- Send request to server
- Fire callback on success
- Update list with item using response data
I want this operation to be fully optimistic:
- Fill input to have name for the item, click submit
- Update list with item using given name
- Send request to server
- Fire callback on success
- Update item in the list with response data
So, in the first example we would simply make an item out of the response and update app state. In second example we make the item and when response comes back, we need to find the right item.
The items have id's when they come back from the server. If item has an id, updating, of course, is very simple. The problem with the second example is, that we don't know the backend id for the item.
I have personally solved the issue by giving a frontend id, which is only used for referring to right element on callbacks. Me and my colleagues don't really like the approach, because it is a bit... messy.
What would be an appropriate, efficient pattern for handling this kind of case?