Skip to content

Commit 1933a4e

Browse files
author
Petr Konecny
committed
fix: fixed reanimated console warnings
1 parent 602a58e commit 1933a4e

File tree

4 files changed

+30
-37
lines changed

4 files changed

+30
-37
lines changed

src/components/AnimatedPagedView/Adapter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useCarouselContext } from '../../context/CarouselContext'
77
import { AutoCarouselAdapterProps } from '../../components/AutoCarousel/types'
88

99
export const AutoCarouselAdapter = ({ offset, onScroll, children }: AutoCarouselAdapterProps) => {
10-
const scrollViewRef = useRef<AnimatedPagedScrollViewRef | null>(null)
10+
const scrollViewRef = useRef<AnimatedPagedScrollViewRef>(null)
1111
const { slideWidth, setUserInteracted } = useCarouselContext()
1212

1313
useAnimatedReaction(

src/components/AnimatedPagedView/index.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { forwardRef, useImperativeHandle, type ForwardedRef } from 'react'
1+
import React, { forwardRef, useImperativeHandle, type ForwardedRef } from 'react'
22
import { Dimensions } from 'react-native'
33
import Animated, {
44
useSharedValue,
@@ -17,15 +17,14 @@ export type AnimatedPagedScrollViewRef = {
1717
scrollTo: (value: number) => void
1818
}
1919

20-
export const AnimatedPagedView = forwardRef(
21-
(
22-
props: {
23-
onScroll: (value: number) => void
24-
onScrollBeginDrag: () => void
25-
children: React.ReactNode
26-
},
27-
ref: ForwardedRef<AnimatedPagedScrollViewRef>,
28-
) => {
20+
type AnimatedPagedViewProps = {
21+
onScroll: (value: number) => void
22+
onScrollBeginDrag: () => void
23+
children: React.ReactNode
24+
}
25+
26+
export const AnimatedPagedView = forwardRef<AnimatedPagedScrollViewRef, AnimatedPagedViewProps>(
27+
(props, ref) => {
2928
const translateX = useSharedValue(0)
3029
const context = useSharedValue({ x: 0 })
3130

@@ -61,14 +60,19 @@ export const AnimatedPagedView = forwardRef(
6160
(value) => {
6261
props.onScroll?.(value)
6362
},
63+
[props.onScroll],
6464
)
6565

66-
useImperativeHandle(ref, () => ({
67-
scrollTo: (value: number) => {
68-
'worklet'
69-
translateX.value = value
70-
},
71-
}))
66+
useImperativeHandle(
67+
ref,
68+
() => ({
69+
scrollTo: (value: number) => {
70+
'worklet'
71+
translateX.value = value
72+
},
73+
}),
74+
[translateX],
75+
)
7276

7377
return (
7478
<GestureDetector gesture={gesture}>

src/components/AutoCarousel/index.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import React, { useRef, useEffect, useCallback } from 'react'
2-
import {
3-
runOnJS,
4-
useAnimatedScrollHandler,
5-
useSharedValue,
6-
withTiming,
7-
useAnimatedReaction,
8-
} from 'react-native-reanimated'
2+
import { runOnJS, useSharedValue, withTiming, useAnimatedReaction } from 'react-native-reanimated'
93

104
import { DEFAULT_INTERVAL, ROUNDING_PRECISION, TRANSITION_DURATION } from './index.preset'
115
import { CarouselContextProvider, useCarouselContext } from '../../context/CarouselContext'
@@ -78,7 +72,7 @@ export const AutoCarouselWithoutProvider = ({
7872
if (!autoScrollEnabled) return
7973
runOnJS(handleAutoScroll)()
8074
},
81-
[scrollValue.value, slideWidth, autoScrollEnabled],
75+
[scrollValue, slideWidth, autoScrollEnabled],
8276
)
8377

8478
// This handles the infinite scrolling
@@ -93,20 +87,12 @@ export const AutoCarouselWithoutProvider = ({
9387
}
9488
// if we are at the first index we need to switch to the next to last one without animation
9589
// next to last one because the last one is a clone of the first one
90+
console.log('activeIndex', activeIndex)
9691
if (activeIndex < 0.01 && activeIndex > -0.01) {
9792
goToPage(paddedChildrenArray.length - 2, 0)
9893
}
9994
},
100-
[childrenArray.length, goToPage, paddedChildrenArray.length, slideWidth, scrollValue.value],
101-
)
102-
103-
const scrollHandler = useAnimatedScrollHandler(
104-
(event) => {
105-
const activeIndex = customRound(event.contentOffset.x / slideWidth, ROUNDING_PRECISION)
106-
if (event.contentOffset.x === 0) return
107-
scrollValue.value = activeIndex
108-
},
109-
[slideWidth, autoScrollEnabled],
95+
[childrenArray.length, goToPage, paddedChildrenArray.length, slideWidth, scrollValue],
11096
)
11197

11298
return (

src/context/CarouselContext/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, useContext, useState } from 'react'
1+
import { createContext, useContext, useMemo, useState } from 'react'
22
import { useSharedValue, type SharedValue } from 'react-native-reanimated'
33

44
import { Dimensions } from 'react-native'
@@ -34,7 +34,10 @@ export const CarouselContextProvider = ({
3434

3535
return (
3636
<CarouselContext.Provider
37-
value={{ scrollValue, userInteracted, setUserInteracted, slideWidth }}
37+
value={useMemo(
38+
() => ({ scrollValue, userInteracted, setUserInteracted, slideWidth }),
39+
[scrollValue, userInteracted, setUserInteracted, slideWidth],
40+
)}
3841
>
3942
{children}
4043
</CarouselContext.Provider>

0 commit comments

Comments
 (0)