I want to use the native driver for dealing animation in React Native app.
I'm using Animated.FlatList
, and I want to define onScroll
outside the render
function.
Here's the code snippet.
class FlatListWithAnimation extends React.PureComponent {
[...]
onScroll = (event) => Animated.event(
[{
nativeEvent: {
contentOffset: { y: this.state.scrollY },
},
}],
{
useNativeDriver: true,
listener: (event, gestureState) => console.log(event, gestureState),
},
)(event);
render() {
[...]
return (
[...]
<Animated.FlatList
key="FlatList"
data={placeHolderData}
renderItem={this.renderPlaceHolder}
onScroll={this.onScroll}
/>
[...]
);
}
}
However, this produce _reactNative.Animated.event(...) is not a function
error. When I put Animated.event
inside directly onScroll={...}
, then it works. But I want to know how I can use Animated.event
outside the render
function.
Thanks.
'useNativeDriver' fixes this by passing all the information about our animations to the UI thread when it starts, meaning that the bridge is no longer needed during the course of the animation. In short, 'useNativeDriver' makes our animations run faster.
The updated answer should be: . start(({finished}) => { if (finished) { console. log('animation ended!) } })
To get the current value of Animated. Value with React Native, we call addListener on the animated value object. to call spinValue. addListener with a callback to get the current value of the animated value from the value property.
Short answer: it isn't possible. You can use native driver only by passing Animated.event
into onScroll
handler.
PanResponder
doesn't support native animations yet, because it's implemented in pure JS.
You can write it like this, but it will work like useNativeDriver: false
onScroll = (event) => Animated.event(
[{
nativeEvent: {
contentOffset: { y: this.state.scrollY },
},
}],
{
useNativeDriver: true,
listener: ({ nativeEvent }) => this.state.scrollY.setValue(nativeEvent.contentOffset.y),
},
).__getHandler()(event);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With