Skip to content

Commit

Permalink
Add default support for item.id as key in FlatList/VList `keyExtrac…
Browse files Browse the repository at this point in the history
…tor`

Summary: [JS] [ENHANCEMENT] - Add default support for `item.id` as key in FlatList/VList `keyExtractor`

Reviewed By: lunaleaps

Differential Revision: D15322879

fbshipit-source-id: 4e03896f72afb05542efc7e960bc29bb07f0178b
  • Loading branch information
sahrens authored and facebook-github-bot committed May 13, 2019
1 parent 0c11d8d commit de0d7cf
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type RequiredProps = {
// `VirtualizedSectionList`'s props.
renderItem: $FlowFixMe<renderItemType>,
/**
* The default accessor functions assume this is an Array<{key: string}> but you can override
* The default accessor functions assume this is an Array<{key: string} | {id: string}> but you can override
* getItem, getItemCount, and keyExtractor to handle any type of index-based data.
*/
data?: any,
Expand Down Expand Up @@ -264,7 +264,7 @@ type State = {first: number, last: number};
* offscreen. This means it's possible to scroll faster than the fill rate ands momentarily see
* blank content. This is a tradeoff that can be adjusted to suit the needs of each application,
* and we are working on improving it behind the scenes.
* - By default, the list looks for a `key` prop on each item and uses that for the React key.
* - By default, the list looks for a `key` or `id` prop on each item and uses that for the React key.
* Alternatively, you can provide a custom `keyExtractor` prop.
*
*/
Expand Down Expand Up @@ -435,6 +435,9 @@ class VirtualizedList extends React.PureComponent<Props, State> {
if (item.key != null) {
return item.key;
}
if (item.id != null) {
return item.id;
}
_usedIndexForKey = true;
if (item.type && item.type.displayName) {
_keylessItemComponentName = item.type.displayName;
Expand Down Expand Up @@ -849,7 +852,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
);
if (!this._hasWarned.keys && _usedIndexForKey) {
console.warn(
'VirtualizedList: missing keys for items, make sure to specify a key property on each ' +
'VirtualizedList: missing keys for items, make sure to specify a key or id property on each ' +
'item or provide a custom keyExtractor.',
_keylessItemComponentName,
);
Expand Down

0 comments on commit de0d7cf

Please sign in to comment.