Repository navigation
Description
Description
I suspect this is related to #5345, but not sure if the same issue. This time I do have a repro.
This bug feels extremely worrying — it's like the most basic expectations don't hold. Am I doing something wrong? We use this API quite a lot so I'm getting nervous about whether it's safe to rely on it.
Steps to reproduce
Try to scroll these two flat lists independently:
import * as React from 'react'
import {View} from 'react-native'
import Animated, {useAnimatedScrollHandler} from 'react-native-reanimated'
export function PagerWithHeader() {
const [x, setX] = React.useState(0)
React.useEffect(() => {
let id = setInterval(() => setX(x => x + 1), 1000)
return () => clearInterval(id)
}, [])
const onScroll = useAnimatedScrollHandler({
onScroll(e) {
console.log(x)
},
})
return (
<View style={{flex: 1, flexDirection: 'row'}}>
<Animated.FlatList
onScroll={onScroll}
style={{flex: 1, backgroundColor: 'red'}}
contentContainerStyle={{
paddingTop: 1000,
}}
/>
<Animated.FlatList
onScroll={onScroll}
style={{flex: 1, backgroundColor: 'blue'}}
contentContainerStyle={{
paddingTop: 1000,
}}
/>
</View>
)
}Android and iOS
Scrolling the first one will output two logs every time:
LOG 179
LOG 0
LOG 179
LOG 0
LOG 179
LOG 0
LOG 179
LOG 0
LOG 179
LOG 0
LOG 179
LOG 0
LOG 179
LOG 0
LOG 179
LOG 0
LOG 180
LOG 0
LOG 180
LOG 0
One of the logs shows the counter being stale.
Scrolling the second one does not have any effect at all—the event does not get fired.
Web
On the web, the event gets fired for both views—but the state inside is always stale.
0
0
0
0
0
In fact, on the web the closure is stale even when passed to only one handler (try deleting one of the flat lists).
Snack
I tried reproducing it in a Snack but couldn't get this working. However, copy pasting the code above into my App.js demonstrates the bug.
GitHub repro
git clone https://github.com/bluesky-social/social-app.git
cd social-app
git checkout reanimated-stuck-onscroll-repro
yarn
yarn start --clear
The related code is in App.native.tsx.
Snack or a link to a repository
Reanimated version
3.4.2
React Native version
0.72.5
Platforms
Android, iOS, Web
JavaScript runtime
Hermes
Workflow
None
Architecture
Paper (Old Architecture)
Build type
None
Device
None
Device model
No response
Acknowledgements
Yes
Activity
[-]Stale closure inside useAnimatedScrollHandler when passed to more than one handler[/-][+]Stale closure inside useAnimatedScrollHandler [/+]tjzel commentedon Dec 17, 2023
Hi @gaearon, it's the same issue.
What's happening here is the following:
useAnimatedScrollHandlerhas its dependencies changed. Therefore it flags its worklet handler as to be rebuild.I gave some more information about using
useAnimatedScrollHandlerwith multiple components here.