1- import React from 'react' ;
1+ import React , { useRef } from 'react' ;
22import type Animated from 'react-native-reanimated' ;
33import { Easing } from '../constants' ;
44import {
55 runOnJS ,
6- useDerivedValue ,
6+ useAnimatedReaction ,
77 useSharedValue ,
88} from 'react-native-reanimated' ;
99import type {
@@ -13,6 +13,7 @@ import type {
1313} from '../types' ;
1414import { dealWithAnimation } from '@/utils/dealWithAnimation' ;
1515import { convertToSharedIndex } from '@/utils/computedWithAutoFillData' ;
16+ import { round } from '@/utils/log' ;
1617
1718interface IOpts {
1819 loop : boolean ;
@@ -28,8 +29,7 @@ interface IOpts {
2829}
2930
3031export interface ICarouselController {
31- sharedIndex : Animated . SharedValue < number > ;
32- sharedPreIndex : Animated . SharedValue < number > ;
32+ getSharedIndex : ( ) => number ;
3333 prev : ( opts ?: TCarouselActionOptions ) => void ;
3434 next : ( opts ?: TCarouselActionOptions ) => void ;
3535 getCurrentIndex : ( ) => number ;
@@ -60,8 +60,8 @@ export function useCarouselController(options: IOpts): ICarouselController {
6060
6161 const index = useSharedValue < number > ( defaultIndex ) ;
6262 // The Index displayed to the user
63- const sharedIndex = useSharedValue < number > ( defaultIndex ) ;
64- const sharedPreIndex = useSharedValue < number > ( defaultIndex ) ;
63+ const sharedIndex = useRef < number > ( defaultIndex ) ;
64+ const sharedPreIndex = useRef < number > ( defaultIndex ) ;
6565
6666 const currentFixedPage = React . useCallback ( ( ) => {
6767 if ( loop ) {
@@ -76,31 +76,46 @@ export function useCarouselController(options: IOpts): ICarouselController {
7676 ) ;
7777 } , [ handlerOffsetX , dataInfo , size , loop ] ) ;
7878
79- useDerivedValue ( ( ) => {
80- const handlerOffsetXValue = handlerOffsetX . value ;
81- sharedPreIndex . value = sharedIndex . value ;
82- const toInt = ( handlerOffsetXValue / size ) % dataInfo . length ;
83- const isPositive = handlerOffsetXValue <= 0 ;
84- const i = isPositive
85- ? Math . abs ( toInt )
86- : Math . abs ( toInt > 0 ? dataInfo . length - toInt : 0 ) ;
87- index . value = i ;
88- sharedIndex . value = convertToSharedIndex ( {
79+ function setSharedIndex ( newSharedIndex : number ) {
80+ sharedIndex . current = newSharedIndex ;
81+ }
82+
83+ useAnimatedReaction (
84+ ( ) => {
85+ const handlerOffsetXValue = handlerOffsetX . value ;
86+ const toInt = round ( handlerOffsetXValue / size ) % dataInfo . length ;
87+ const isPositive = handlerOffsetXValue <= 0 ;
88+ const i = isPositive
89+ ? Math . abs ( toInt )
90+ : Math . abs ( toInt > 0 ? dataInfo . length - toInt : 0 ) ;
91+
92+ const newSharedIndexValue = convertToSharedIndex ( {
93+ loop,
94+ rawDataLength : dataInfo . originalLength ,
95+ autoFillData : autoFillData ! ,
96+ index : i ,
97+ } ) ;
98+
99+ return {
100+ i,
101+ newSharedIndexValue,
102+ } ;
103+ } ,
104+ ( { i, newSharedIndexValue } ) => {
105+ index . value = i ;
106+ runOnJS ( setSharedIndex ) ( newSharedIndexValue ) ;
107+ } ,
108+ [
109+ sharedPreIndex ,
110+ sharedIndex ,
111+ size ,
112+ dataInfo ,
113+ index ,
89114 loop ,
90- rawDataLength : dataInfo . originalLength ,
91- autoFillData : autoFillData ! ,
92- index : i ,
93- } ) ;
94- } , [
95- sharedPreIndex ,
96- sharedIndex ,
97- size ,
98- dataInfo ,
99- index ,
100- loop ,
101- autoFillData ,
102- handlerOffsetX ,
103- ] ) ;
115+ autoFillData ,
116+ handlerOffsetX ,
117+ ]
118+ ) ;
104119
105120 const getCurrentIndex = React . useCallback ( ( ) => {
106121 return index . value ;
@@ -255,12 +270,11 @@ export function useCarouselController(options: IOpts): ICarouselController {
255270 ) ;
256271
257272 return {
258- sharedIndex,
259- sharedPreIndex,
260273 to,
261274 next,
262275 prev,
263276 scrollTo,
264277 getCurrentIndex,
278+ getSharedIndex : ( ) => sharedIndex . current ,
265279 } ;
266280}
0 commit comments