From f9c66e185ccf95ef0ac17c5f9fb1e36f0ed7c77f Mon Sep 17 00:00:00 2001 From: Jayesh Mangwani Date: Wed, 18 Oct 2023 14:46:33 +0530 Subject: [PATCH 1/4] added allOptions to useCallback dependencies --- src/components/SelectionList/BaseSelectionList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/SelectionList/BaseSelectionList.js b/src/components/SelectionList/BaseSelectionList.js index fdb1f92ca73b..685c6cb7c0d4 100644 --- a/src/components/SelectionList/BaseSelectionList.js +++ b/src/components/SelectionList/BaseSelectionList.js @@ -176,7 +176,7 @@ function BaseSelectionList({ // If we don't disable dependencies here, we would need to make sure that the `sections` prop is stable in every usage of this component. // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + }, [flattenedSections.allOptions]); /** * Logic to run when a row is selected, either with click/press or keyboard hotkeys. From a5980135dbb4175fcae978585e272fb26a9bfe93 Mon Sep 17 00:00:00 2001 From: Jayesh Mangwani Date: Wed, 18 Oct 2023 15:13:48 +0530 Subject: [PATCH 2/4] fix: prettier diff detected --- .../SelectionList/BaseSelectionList.js | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/components/SelectionList/BaseSelectionList.js b/src/components/SelectionList/BaseSelectionList.js index 685c6cb7c0d4..47a947bc2db5 100644 --- a/src/components/SelectionList/BaseSelectionList.js +++ b/src/components/SelectionList/BaseSelectionList.js @@ -152,31 +152,34 @@ function BaseSelectionList({ * @param {Number} index - the index of the item to scroll to * @param {Boolean} animated - whether to animate the scroll */ - const scrollToIndex = useCallback((index, animated = true) => { - const item = flattenedSections.allOptions[index]; + const scrollToIndex = useCallback( + (index, animated = true) => { + const item = flattenedSections.allOptions[index]; - if (!listRef.current || !item) { - return; - } + if (!listRef.current || !item) { + return; + } - const itemIndex = item.index; - const sectionIndex = item.sectionIndex; + const itemIndex = item.index; + const sectionIndex = item.sectionIndex; - // Note: react-native's SectionList automatically strips out any empty sections. - // So we need to reduce the sectionIndex to remove any empty sections in front of the one we're trying to scroll to. - // Otherwise, it will cause an index-out-of-bounds error and crash the app. - let adjustedSectionIndex = sectionIndex; - for (let i = 0; i < sectionIndex; i++) { - if (_.isEmpty(lodashGet(sections, `[${i}].data`))) { - adjustedSectionIndex--; + // Note: react-native's SectionList automatically strips out any empty sections. + // So we need to reduce the sectionIndex to remove any empty sections in front of the one we're trying to scroll to. + // Otherwise, it will cause an index-out-of-bounds error and crash the app. + let adjustedSectionIndex = sectionIndex; + for (let i = 0; i < sectionIndex; i++) { + if (_.isEmpty(lodashGet(sections, `[${i}].data`))) { + adjustedSectionIndex--; + } } - } - listRef.current.scrollToLocation({sectionIndex: adjustedSectionIndex, itemIndex, animated, viewOffset: variables.contentHeaderHeight}); + listRef.current.scrollToLocation({sectionIndex: adjustedSectionIndex, itemIndex, animated, viewOffset: variables.contentHeaderHeight}); - // If we don't disable dependencies here, we would need to make sure that the `sections` prop is stable in every usage of this component. - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [flattenedSections.allOptions]); + // If we don't disable dependencies here, we would need to make sure that the `sections` prop is stable in every usage of this component. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, + [flattenedSections.allOptions], + ); /** * Logic to run when a row is selected, either with click/press or keyboard hotkeys. From db319a7b9114bdbb3b9489def5bcf04051ae57ac Mon Sep 17 00:00:00 2001 From: Jayesh Mangwani Date: Wed, 18 Oct 2023 16:42:26 +0530 Subject: [PATCH 3/4] added sections to scrollToIndex dependencies --- src/components/SelectionList/BaseSelectionList.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/components/SelectionList/BaseSelectionList.js b/src/components/SelectionList/BaseSelectionList.js index 47a947bc2db5..a2f3661f0c59 100644 --- a/src/components/SelectionList/BaseSelectionList.js +++ b/src/components/SelectionList/BaseSelectionList.js @@ -174,11 +174,8 @@ function BaseSelectionList({ } listRef.current.scrollToLocation({sectionIndex: adjustedSectionIndex, itemIndex, animated, viewOffset: variables.contentHeaderHeight}); - - // If we don't disable dependencies here, we would need to make sure that the `sections` prop is stable in every usage of this component. - // eslint-disable-next-line react-hooks/exhaustive-deps }, - [flattenedSections.allOptions], + [flattenedSections.allOptions, sections], ); /** From c51b66730ac14dec9fe6a86cba3b89c7d699acd3 Mon Sep 17 00:00:00 2001 From: Jayesh Mangwani Date: Fri, 20 Oct 2023 11:54:02 +0530 Subject: [PATCH 4/4] dsable exhaustive-deps for scrollToIndex --- src/components/SelectionList/BaseSelectionList.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/SelectionList/BaseSelectionList.js b/src/components/SelectionList/BaseSelectionList.js index a2f3661f0c59..585871b4cc83 100644 --- a/src/components/SelectionList/BaseSelectionList.js +++ b/src/components/SelectionList/BaseSelectionList.js @@ -175,7 +175,9 @@ function BaseSelectionList({ listRef.current.scrollToLocation({sectionIndex: adjustedSectionIndex, itemIndex, animated, viewOffset: variables.contentHeaderHeight}); }, - [flattenedSections.allOptions, sections], + + // eslint-disable-next-line react-hooks/exhaustive-deps + [flattenedSections.allOptions], ); /**