Using indexes as a key
In Chapter 9, Improve the Performance of Your Applications, talking about performance and the reconciler, we have seen how we can help React figure out the shortest path to update the DOM by using the key
prop.
The key prop uniquely identifies an element in the DOM and React uses it to check if the element is new or if it has to be updated when the component props or state change.
Using keys is always a good idea and, if you don't do it, React gives a warning in the console (in development mode). However, it is not simply a matter of using a key; sometimes the value that we decide to use as a key can make the difference. In fact, using the wrong key can give us unexpected behaviors in some instances. In this section, we will see one of those instances.
Let's, again, create a List
component:
class List extends React.PureComponent
In the constructor, the items are initialized and the handlers bound to the component:
constructor(props) { super(props) this...