Skip to content

Commit 46190bc

Browse files
Skipperllaenestatli
authored andcommitted
refactor: removed initialScrollIndex and added onContentSizeChange instead
1 parent 35de254 commit 46190bc

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/components/RulerPicker.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {
1010
} from 'react-native';
1111
import type { NativeSyntheticEvent, NativeScrollEvent } from 'react-native';
1212

13-
import { AnimatedFlashList, ListRenderItem } from '@shopify/flash-list';
13+
import {
14+
AnimatedFlashList,
15+
FlashList,
16+
ListRenderItem,
17+
} from '@shopify/flash-list';
1418

1519
import { RulerPickerItem, RulerPickerItemProps } from './RulerPickerItem';
1620
import { calculateCurrentValue } from '../utils/';
@@ -141,6 +145,7 @@ export const RulerPicker = ({
141145
}: RulerPickerProps) => {
142146
const itemAmount = (max - min) / step;
143147
const arrData = Array.from({ length: itemAmount + 1 }, (_, index) => index);
148+
const listRef = useRef<FlashList<typeof arrData>>(null);
144149

145150
const stepTextRef = useRef<TextInput>(null);
146151
const prevValue = useRef<string>(initialValue.toFixed(fractionDigits));
@@ -253,10 +258,19 @@ export const RulerPicker = ({
253258
step,
254259
]
255260
);
261+
const onContentSizeChange = useCallback(() => {
262+
const initialIndex = Math.floor((initialValue - min) / step);
263+
listRef.current?.scrollToOffset({
264+
offset: initialIndex * (stepWidth + gapBetweenSteps),
265+
animated: false,
266+
});
267+
// eslint-disable-next-line react-hooks/exhaustive-deps
268+
}, []);
256269

257270
return (
258271
<View style={{ width, height }}>
259272
<AnimatedFlashList
273+
ref={listRef}
260274
data={arrData}
261275
keyExtractor={(_, index) => index.toString()}
262276
renderItem={renderItem}
@@ -268,7 +282,7 @@ export const RulerPicker = ({
268282
snapToOffsets={arrData.map(
269283
(_, index) => index * (stepWidth + gapBetweenSteps)
270284
)}
271-
initialScrollIndex={Math.floor((initialValue - min) / step)}
285+
onContentSizeChange={onContentSizeChange}
272286
snapToAlignment="start"
273287
decelerationRate={decelerationRate}
274288
estimatedFirstItemOffset={0}

0 commit comments

Comments
 (0)