Skip to content

Commit 235b6b1

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: Fix Unstable Component in FlatList-multiColumn (#51673)
Summary: Pull Request resolved: #51673 Fixes a lint warning that seems to inconsistently fire, by improving the code that it complains about. Changelog: [Internal] Reviewed By: javache Differential Revision: D75583443 fbshipit-source-id: b6df191e2e51dee688df3f3c960704dea28a4ece
1 parent a42631e commit 235b6b1

1 file changed

Lines changed: 28 additions & 25 deletions

File tree

packages/rn-tester/js/examples/FlatList/FlatList-multiColumn.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
import RNTesterPage from '../../components/RNTesterPage';
2929
import RNTesterText from '../../components/RNTesterText';
3030
import * as React from 'react';
31-
import {useState} from 'react';
31+
import {useCallback, useState} from 'react';
3232
import {Alert, FlatList, StyleSheet, View} from 'react-native';
3333

3434
function MultiColumnExample(): React.Node {
@@ -66,20 +66,33 @@ function MultiColumnExample(): React.Node {
6666
return {length, offset: length * index, index};
6767
};
6868

69-
// eslint-disable-next-line react/no-unstable-nested-components
70-
const _renderItemComponent = ({
71-
item,
72-
}: ListRenderItemInfo<any | Item>): $FlowFixMe => {
73-
return (
74-
<View style={styles.card}>
75-
<ItemComponent
76-
item={item}
77-
fixedHeight={fixedHeight}
78-
onPress={_pressItem}
79-
/>
80-
</View>
81-
);
82-
};
69+
const _pressItem = useCallback(
70+
(key: string) => {
71+
const index = Number(key);
72+
const itemState = pressItem(data[index]);
73+
setData(state => [
74+
...state.slice(0, index),
75+
itemState,
76+
...state.slice(index + 1),
77+
]);
78+
},
79+
[data],
80+
);
81+
82+
const _renderItemComponent = useCallback(
83+
({item}: ListRenderItemInfo<any | Item>): $FlowFixMe => {
84+
return (
85+
<View style={styles.card}>
86+
<ItemComponent
87+
item={item}
88+
fixedHeight={fixedHeight}
89+
onPress={_pressItem}
90+
/>
91+
</View>
92+
);
93+
},
94+
[_pressItem, fixedHeight],
95+
);
8396

8497
// This is called when items change viewability by scrolling into or out of the viewable area.
8598
const _onViewableItemsChanged = (info: {
@@ -102,16 +115,6 @@ function MultiColumnExample(): React.Node {
102115
}
103116
};
104117

105-
const _pressItem = (key: string) => {
106-
const index = Number(key);
107-
const itemState = pressItem(data[index]);
108-
setData(state => [
109-
...state.slice(0, index),
110-
itemState,
111-
...state.slice(index + 1),
112-
]);
113-
};
114-
115118
const filterRegex = new RegExp(String(filterText), 'i');
116119
const filter = (item: any | Item) =>
117120
filterRegex.test(item.text) || filterRegex.test(item.title);

0 commit comments

Comments
 (0)