Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React list with no keys

I have an array of number that I wish to render in a tabular form. The array is returned from an API call, not generated by my app.

The data may change but is unlikely to do so, and in any case there are only twenty odd values, so re-rendering the whole table is not really a problem.

A simple data.map(value => <td>{value}</td> should do it.

But I keep getting an Each child in an array or iterator should have a unique "key" prop. warning. Is there any way that I can tell React that there is no key and that I wish it to re-render the whole table if anything changes.

Alternatively, is there any way that I can generate a unique key for each entry? The data items are not guaranteed to be unique.

I should add that I understand what keys are for and why they are useful, but in this instance I do not have any and the easiest thing would be not to use them, since there is unlikely to be a re-render.

like image 974
amay Avatar asked Jan 10 '18 18:01

amay


1 Answers

You can use the index as the key. I think its worth reiterating that using the index as the key only works fine in the very specific scenario that the OP is facing.

This is particularly annoying when requirements change and all of sudden the list is being modified. This shows up as items not being updated during the render because the item updated has the same key (the index), its value is different, but react only cares about the key.

In cases where your data has no unique key. You should use some function that generates a unique id for each item. A simple version of that function just increments a global counter:

// Declared globally (as in attached to window object or equivalent)
var myuniqueidcounter = 0;
function uniqueId() {
    myuniqueidcounter += 1
    return myuniqueidcounter;
}


// Do this in the props change or whereever your data gets passed in
let keyedData = data.map(value => Object.assign(value, { Id: uniqueId() });

// In render
data.map(value => <td key={value.Id}>{value}</td>

That way, on multiple render calls, the ids returned are always unique. We assign the key when we get the data to avoid having to re-render the entire list on each call to render().

However, this case is actually pretty rare as you can usually find some combination of the backing data that will produce a unique key for each entry.

If you do go index-as-key

This article lists 3 conditions that should be met when choosing index-as-key approach that I think is a good check list:

  1. The list and items are static–they are not computed and do not change;

  2. The items in the list have no ids;

  3. The list is never reordered or filtered.

like image 84
jmathew Avatar answered Sep 28 '22 07:09

jmathew