Skip to content

Commit

Permalink
fix: removed the reset logic when user change the defaultIndex prop
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Dec 3, 2023
1 parent c015873 commit 6b849ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-pianos-pull.md
@@ -0,0 +1,5 @@
---
'react-native-reanimated-carousel': patch
---

Removed the reset logic when user change the defaultIndex prop. (We couldn't update the handlerOffset value when user change the defaultIndex. Because the carousel component already be a non-controlled component. So the subsequent changes of defaultIndex will be ignored.)
11 changes: 4 additions & 7 deletions src/hooks/useCommonVariables.ts
@@ -1,4 +1,3 @@
import React from "react";
import type Animated from "react-native-reanimated";
import { useSharedValue, useAnimatedReaction } from "react-native-reanimated";

Expand Down Expand Up @@ -26,16 +25,14 @@ export function useCommonVariables(
loop,
} = props;
const size = vertical ? height : width;
const validLength = dataLength - 1;
const defaultHandlerOffsetValue = -Math.abs(defaultIndex * size);
const _handlerOffset = useSharedValue<number>(defaultHandlerOffsetValue);
const handlerOffset = defaultScrollOffsetValue ?? _handlerOffset;
const prevDataLength = useSharedValue(dataLength);

React.useEffect(() => {
handlerOffset.value = defaultHandlerOffsetValue;
}, [vertical, handlerOffset, defaultHandlerOffsetValue]);

/**
* When data changes, we need to compute new index for handlerOffset
*/
useAnimatedReaction(() => {
const previousLength = prevDataLength.value;
const currentLength = dataLength;
Expand Down Expand Up @@ -67,7 +64,7 @@ export function useCommonVariables(

return {
size,
validLength,
validLength: dataLength - 1,
handlerOffset,
};
}

0 comments on commit 6b849ec

Please sign in to comment.