question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shared value updates inside of onEndDrag does not appear to trigger useDerivedValue hook.

See original GitHub issue

Description

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:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Eljoycommented, Nov 29, 2021

@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.

1reaction
hannojgcommented, Nov 28, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found