Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Animated with useNativeDriver - _reactNative.Animated.event(...) is not a function

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.

like image 613
Junhee Park Avatar asked Mar 08 '19 02:03

Junhee Park


People also ask

What is the use of useNativeDriver in React Native?

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

How do you check if animation is finished React Native?

The updated answer should be: . start(({finished}) => { if (finished) { console. log('animation ended!) } })

How do I get animated value React Native?

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.


1 Answers

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);
like image 193
Vitaliy Leonov Avatar answered Sep 22 '22 12:09

Vitaliy Leonov