Performance drop with FlatList + useDerivedValue
See original GitHub issueDescription
FlatList experiences a serious drop in performance when useDerivedValue is used inside the renderItem function (but only if we return shared value, if inside useDerivedValue return null everything is ok).
Screenshots
Steps To Reproduce
Run code below. Then change return value of useDerivedValue to null - after that everything should be smooth .
Expected behavior
Smooth 60 fps.
Actual behavior
If I scroll back and forth i can drop performance to ~ 3 fps.
Snack or minimal code example
import React from 'react'
import {StyleSheet, FlatList, View, Text} from 'react-native'
import Animated, {
useAnimatedScrollHandler,
useSharedValue,
useDerivedValue
} from 'react-native-reanimated'
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList)
const data = [...Array(300).keys()]
const Slider = () => {
const transX = useSharedValue(0)
const scrollHandler = useAnimatedScrollHandler({
onScroll: (event) => {
transX.value = event.contentOffset.x
}
})
const renderItem = ({item, index}) => {
return <Item index={index} item={item} transX={transX} />
}
return (
<View style={styles.container}>
<View style={styles.listContainer}>
<AnimatedFlatList
onScroll={scrollHandler}
horizontal
showsHorizontalScrollIndicator={false}
style={styles.list}
data={data}
decelerationRate="fast"
centerContent
snapToInterval={15}
scrollEventThrottle={16}
snapToAlignment="center"
renderItem={renderItem}
keyExtractor={(item, index) => {String(index)}}
/>
</View>
</View>
)
}
const Item = ({index, item, transX}) => {
const udv = useDerivedValue(() => {
return transX.value //change this to null
})
return (
<Animated.View style={[styles.box]}>
<Text style={styles.label}>{item}</Text>
</Animated.View>
)}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
listContainer: {
height: 265,
alignItems: 'center',
justifyContent: 'center',
},
list: {
height: 240,
flexGrow: 0,
},
box: {
width: 15,
height: 150,
backgroundColor: 'blue',
borderRadius: 20,
alignItems: 'center',
justifyContent: 'center',
borderColor:'black',
borderWidth:1
},
label: {
fontSize: 10,
color: '#fff',
},
})
export default Slider
Package versions
- React: 16.13.1
- React Native: 0.63.4
- React Native Reanimated: 2.0.0-rc.3
- NodeJS: 14.15.5
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Optimizing Flatlist Configuration - React Native
Pros: This reduces time spent on the main thread, and thus reduces the risk of dropped frames, by excluding views outside of the...
Read more >How to Make Performant Animations in React Native using ...
Reanimated enables us to create performant animations and user interactions that run quickly on the UI thread without any frame drops.
Read more >Create high-performance graphics with React Native Skia
Thanks to the dev team behind react-native-skia, we can now use Skia in React Native applications to draw graphics and create UI concepts....
Read more >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 >scrolleventthrottle flatlist - You.com | The search engine you control.
FlatList experiences a serious drop in performance when useDerivedValue is used inside the renderItem function (but only if we return shared value, ...
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

The same thing happens with simple manual scroll, so that doesn’t related to FlatList by itself.
I couldn’t find any so I reverted to Animated library