⊗jsrtPmIdPm 45 of 112 menu

Problems with new ids in React

Let's say we receive the following array of objects from the database:

const prods = [ {id: 1, name: 'product1', cost: 100}, {id: 2, name: 'product2', cost: 200}, {id: 3, name: 'product3', cost: 300}, ];

As you can see, id are numbered sequentially. However, we should take into account that the numbers may have gaps, for example, after 3-th there may immediately be 5-th or 6-th. Because of this, our client script cannot know what the next id will be (in our case, it is not necessarily 4).

The new id are created by the server database. This can cause some problems when working on the client. The point is this: imagine that we added a new element to our array using a form. However, we can't just go ahead and add the data from the form - because we don't know what id the new element will have!

We will need to send a request to the server to give us the next id in order, and only then add the element to our array of objects. This will cause a delay in displaying the data on the screen: while the data arrives to the server, while the server sends it back to us - some time will pass.

enru