Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Animated.Event work in React Native?

Tags:

react-native

I'm I understanding it correctly ? Does this two set of code meant the same thing ?Does it have any difference in performance or reliability ?

<ScrollView
 onScroll={Animated.event(
  [{nativeEvent: {contentOffset: {y: this.state.scrollY}}}]
)}
>
</ScrollView>

AND

handleScroll(e){
  this.setState({ scrollY : e.nativeEvent.contentOffset.y });
}

<ScrollView
 onScroll={(e) => this.handleScroll(e)}
>
</ScrollView>

Thanks

like image 306
cjmling Avatar asked Apr 20 '17 04:04

cjmling


People also ask

How do animations work in React Native?

Well, simply put, it means that React Native offloads the animation work from the JS thread to the UI thread (the OS) and lets it handle the animation of the object. This has a couple of benefits: The JS thread (and the React Native bridge) is now free for handling other intensive tasks like repetitive taps from user.

How do you get Animated values in 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.


4 Answers

If you want to handle the scroll you can use it this way:

handleScroll = (event) => {
    //custom actions
}

<ScrollView
 onScroll={Animated.event(
[{ nativeEvent: {
    contentOffset: {
      x: this.state.scrollY
     }
  }
}],{
   listener: event => {
       this.handleScroll(event);
   }})
}>
</ScrollView>
like image 197
Fromwood Avatar answered Sep 28 '22 15:09

Fromwood


it's not the same. Animated.event is used to map gestures like scrolling, panning or other events directly to Animated values. so in your first example this.state.scrollY is an Animated.Value. you would probably have code somewhere that initialized it, maybe your constructor would looks something like this:

constructor(props) {
  super(props);
  this.state = {
    scrollY: new Animated.Value(0)
  };
}

in your second example this.state.scrollY is the y value (just the number) that was triggered in the scroll event, but completely unrelated to animation. so you couldn't use that value as you could use Animated.Value in animation.

it's explained here in the documentation

like image 45
Zohar Levin Avatar answered Sep 27 '22 15:09

Zohar Levin


According to this source code Animated.event traverses the objects passed as arguments of it until finds an instance of AnimatedValue.

Then this key (where the AnimatedValue has been found) is applied to the callback (onScroll) and the value at the key of the event passed to the scroll is assigned to this AnimatedValue.

In the code:

const animatedValue = useRef(new Animated.Value(0)).current;
...
onScroll={Animated.event(
  [{nativeEvent: {contentOffset: {y: animatedValue}}}]
)}

is the same as

const animatedValue = useRef(new Animated.Value(0)).current;
...
onScroll={({nativeEvent: { contentOffset: {y} }}) => {
  animatedValue.setValue(y);
}}

If your callback accepts more than one event (argument), then just put the mapping object at the needed index (thus the array as the argument of Animated.value.

onScroll={Animated.event(
  [
     {}, // <- disregard first argument of the event callback
     {nativeEvent: {contentOffset: {y: animatedValue}}} // <- apply mapping to the second 
  ]
)}


like image 44
AlVelig Avatar answered Sep 29 '22 15:09

AlVelig


Yes there is a difference in semantic

 <ScrollView onScroll={Animated.event(
  [{nativeEvent: {contentOffset: {y: this.state.scrollY}}}]
)}></ScrollView>

The first one i.e the above Animated.event returns a function that sets the scrollview's nativeEvent.contentOffset.y to your current scrollY state which I assume is animated.

The other code just sets scrollY to your scrollView's e.nativeEvent.contentOffset.y and causes a rerender to your component

like image 33
kabangi julius Avatar answered Sep 29 '22 15:09

kabangi julius