Discussion of unique ids in React
As you already know, the array of objects must contain unique id
. Let's figure out where they come from.
Arrays of objects can have two origins: either they are sent to us by the server, or they are generated on the client (i.e. in the browser).
The data sent to us by the server was usually stored there in a database. Databases (DB) can be of the SQL type (e.g. mySQL, PostgreSQL) or NoSQL (e.g. MongoDB).
SQL Databases typically have numeric id
, automatically arranged by the database in ascending order.
NoSQL Databases typically have id
, which are random unique rows. These rows are assumed to have no overlaps (collisions) between two array elements.
The uniqueness of id
is achieved by a sufficiently large length of random strings - so large that the probability of two strings matching will be close to zero.
In this case, the more data in the database, the greater the probability of a collision. The task of the programmer is to estimate the volume of data in advance and determine the optimal length of random strings so that the probability of collisions is minimal (small enough to be considered acceptable).