Skip to content

Commit 6d562a6

Browse files
committed
Fixed null checks when rendering section list
1 parent 66a8a18 commit 6d562a6

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

packages/core/src/components/SectionList/SectionList.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ interface SectionListSectionItem {
3333

3434
type SectionListItem<T> = SectionListDataItem<T> | SectionListSectionItem;
3535

36+
const DEFAULT_SECTION = "Uncategorized";
37+
3638
const SectionList = <T extends { [key: string]: any }>({
3739
sectionKey,
3840
listComponent = "FlatList",
@@ -47,7 +49,7 @@ const SectionList = <T extends { [key: string]: any }>({
4749
const sectionDataItems: { [key: string]: T[] } = {};
4850

4951
for (const item of data) {
50-
const section = item[sectionKey]?.toString() || "Uncategorized";
52+
const section = item[sectionKey]?.toString() || DEFAULT_SECTION;
5153
if (sectionDataItems[section]) {
5254
sectionDataItems[section].push(item);
5355
} else {
@@ -66,8 +68,14 @@ const SectionList = <T extends { [key: string]: any }>({
6668
return result;
6769
}, [data, sectionKey]);
6870

69-
const extractSectionHeader = (element: JSX.Element): JSX.Element | null => {
70-
const props = element?.props || {};
71+
const extractSectionHeader = (
72+
element: JSX.Element | null
73+
): JSX.Element | null => {
74+
if (!element) {
75+
return null;
76+
}
77+
78+
const props = element.props || {};
7179
const children = React.Children.toArray(props.children).map(
7280
(child) => child as React.ReactElement
7381
);
@@ -84,9 +92,13 @@ const SectionList = <T extends { [key: string]: any }>({
8492
};
8593

8694
const extractRemainingNonSectionHeader = (
87-
element: JSX.Element
95+
element: JSX.Element | null
8896
): JSX.Element | null => {
89-
const props = element?.props || {};
97+
if (!element) {
98+
return null;
99+
}
100+
101+
const props = element.props || {};
90102
const children = React.Children.toArray(props.children).map(
91103
(child) => child as React.ReactElement
92104
);
@@ -129,7 +141,7 @@ const SectionList = <T extends { [key: string]: any }>({
129141
const renderedItem = renderItemProp({
130142
item: item.data,
131143
index,
132-
section: item.data[sectionKey],
144+
section: item.data[sectionKey] || DEFAULT_SECTION,
133145
});
134146
return extractRemainingNonSectionHeader(renderedItem);
135147
}

0 commit comments

Comments
 (0)