Shared value updates inside of onEndDrag does not appear to trigger useDerivedValue hook.
See original GitHub issueDescription
Shared value updates inside of onEndDrag does not appear to trigger useDerivedValue hook.
Expected behavior
scroll shared value changes inside onEndDrag should trigger useDerivedValue callback which snaps scroll to previous or next item.
Actual behavior & steps to reproduce
scroll shared value changes don’t trigger useDerivedValue callback
Snack or minimal code example
export function ProfileInfoScrollView({
}: ProfileInfoScrollView.Props) {
const { profiles } = useProfile()
const [containerHeight, setContainerHeight] = useState(0)
const aref = useAnimatedRef<FlatList>()
const scroll = useSharedValue(0)
useDerivedValue(() => {
scrollTo(aref as any, 0, scroll.value * containerHeight, true)
})
const animatedScrollHandler = useAnimatedScrollHandler<{
beginOffset: number
}>({
onBeginDrag: (e, c) => {
c.beginOffset = e.contentOffset.y
},
onEndDrag: (e, c) => {
const currentOffset = e.contentOffset.y
const direction = currentOffset > c.beginOffset ? 'down' : 'up'
scroll.value = direction === 'up' ? scroll.value - 1 : scroll.value + 1
},
})
return (
<View
onLayout={(event) => {
const { height } = event.nativeEvent.layout
setContainerHeight(height)
}}>
<Button
title="scroll down"
onPress={() => {
scroll.value = scroll.value + 1
if (scroll.value >= 10) scroll.value = 0
}}
/>
<AnimatedFlatList
ref={aref}
data={profiles}
onScroll={animatedScrollHandler}
getItemLayout={(_, index) => {
return {
length: containerHeight,
index,
offset: index * containerHeight,
}
}}
scrollEventThrottle={16}
renderItem={({ item: p }: ListRenderItemInfo<Profile>) => (
<ProfileInfo height={containerHeight} key={p.id} profile={p} />
)}
keyExtractor={(p: Profile) => p.id}
/>
</View>
)
}
Package versions
- React Native: 0.66.2
- React Native Reanimated: 2.2.4, 2.3.0-beta.4
- NodeJS:
- Xcode:
- Java & Gradle:
Affected platforms
- Android
- iOS
- Web
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
useDerivedValue | React Native Reanimated
This hook allows for creating shared value reference that can change in response to updating of one or more other shared values.
Read more >Moti vs. Reanimated
To use Reanimated shared values with Moti, you can pass useDerivedValue to the animate prop. This allows the animate prop to be fully...
Read more >reactjs - How do i trigger a useEffect based on a sharedValue ...
useSharedValue in the Reanimated v2 library actually returns a reference and in react useEffect does not trigger on mutation in reference ...
Read more >Shared Values - React Native Reanimated
Values . They serve a similar purpose of carrying "animateable" data, providing a notion of reactiveness, and driving animations. We will discuss each...
Read more >Deep dive into React Native Reanimated - LogRocket Blog
When data inside of these Share Values is updated, the Reanimated library ... A worklet is triggered by any change to the Shared...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@tomekzaw Well, the useSharedValue seems to be triggered. Although now I see that there is an issue of scrollTo function not working when there is still an active scroll momentum. Thanks.
A temporary fix for me is to uncomment the lines in the file mentioned in this issue https://github.com/software-mansion/react-native-reanimated/issues/2667