Skip to content

Commit

Permalink
fix: focused index is not persisted when carousel component is re-ren…
Browse files Browse the repository at this point in the history
…dered

fix #181
  • Loading branch information
dohooo committed May 15, 2022
1 parent 59fb446 commit d82d39b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
46 changes: 24 additions & 22 deletions src/hooks/useCarouselController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type Animated from 'react-native-reanimated';
import { Easing } from '../constants';
import {
runOnJS,
useAnimatedReaction,
useDerivedValue,
useSharedValue,
} from 'react-native-reanimated';
import type {
Expand Down Expand Up @@ -76,27 +76,29 @@ export function useCarouselController(options: IOpts): ICarouselController {
);
}, [handlerOffsetX, dataInfo, size, loop]);

const computedIndex = React.useCallback(
(handlerOffsetXValue: number) => {
'worklet';
sharedPreIndex.value = sharedIndex.value;
const toInt = (handlerOffsetXValue / size) % dataInfo.length;
const isPositive = handlerOffsetXValue <= 0;
const i = isPositive
? Math.abs(toInt)
: Math.abs(toInt > 0 ? dataInfo.length - toInt : 0);
index.value = i;
sharedIndex.value = convertToSharedIndex({
loop,
rawDataLength: dataInfo.originalLength,
autoFillData: autoFillData!,
index: i,
});
},
[sharedPreIndex, sharedIndex, size, dataInfo, index, loop, autoFillData]
);

useAnimatedReaction(() => handlerOffsetX.value, computedIndex, [
useDerivedValue(() => {
const handlerOffsetXValue = handlerOffsetX.value;
sharedPreIndex.value = sharedIndex.value;
const toInt = (handlerOffsetXValue / size) % dataInfo.length;
const isPositive = handlerOffsetXValue <= 0;
const i = isPositive
? Math.abs(toInt)
: Math.abs(toInt > 0 ? dataInfo.length - toInt : 0);
index.value = i;
sharedIndex.value = convertToSharedIndex({
loop,
rawDataLength: dataInfo.originalLength,
autoFillData: autoFillData!,
index: i,
});
}, [
sharedPreIndex,
sharedIndex,
size,
dataInfo,
index,
loop,
autoFillData,
handlerOffsetX,
]);

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useCommonVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ interface ICommonVariables {
export function useCommonVariables(
props: TInitializeCarouselProps<any>
): ICommonVariables {
const { vertical, height, width, data, defaultIndex, rawData } = props;
const { vertical, height, width, data, defaultIndex } = props;
const size = vertical ? height : width;
const validLength = data.length - 1;
const defaultHandlerOffsetX = -Math.abs(defaultIndex * size);
const handlerOffsetX = useSharedValue<number>(defaultHandlerOffsetX);

React.useEffect(() => {
handlerOffsetX.value = defaultHandlerOffsetX;
}, [vertical, handlerOffsetX, defaultHandlerOffsetX, rawData]);
}, [vertical, handlerOffsetX, defaultHandlerOffsetX]);

return {
size,
Expand Down

0 comments on commit d82d39b

Please sign in to comment.