Skip to content

Commit 71ee5be

Browse files
MelvinBotmkhutornyi
andcommitted
Fix: defer initiallyFocusedKey clear by one frame to let FlashList scroll
The immediate setState in useEffect caused FlashList to see initialScrollIndex change from truthy to falsy before it processed the initial scroll. Using requestAnimationFrame gives FlashList one frame to complete the scroll before the key is cleared. Co-authored-by: mkhutornyi <mkhutornyi@users.noreply.github.com>
1 parent 67a2437 commit 71ee5be

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/components/Search/SearchMultipleSelectionPicker.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ function SearchMultipleSelectionPicker<T extends string | string[]>({
4747
return minItem?.name;
4848
});
4949

50-
// Clear after first render to prevent FlashList from auto-scrolling when data changes
50+
// Clear after mount to prevent FlashList from auto-scrolling when data changes
5151
// cause the key to transition from "not found" to "found" (e.g., clearing a search).
52+
// Deferred by one frame so FlashList processes the initial scroll first.
5253
const [initiallyFocusedKey, setInitiallyFocusedKey] = useState(initiallyFocusedKeyComputed);
5354
useEffect(() => {
54-
// eslint-disable-next-line react-hooks/set-state-in-effect -- One-time clear after mount; prevents FlashList auto-scrolling when data changes cause the key to re-match
55-
setInitiallyFocusedKey(undefined);
55+
const id = requestAnimationFrame(() => {
56+
setInitiallyFocusedKey(undefined);
57+
});
58+
return () => cancelAnimationFrame(id);
5659
}, []);
5760

5861
const searchLower = debouncedSearchTerm.toLowerCase();

0 commit comments

Comments
 (0)