How to use requestAnimationFrame in react native.
I use this for the performance of TouchableOpacity like this
this.requestAnimationFrame(() => {
if (this.state.scrollEnabled)
this._panel.transitionTo({toValue: 0});
else {
this.setState({scrollEnabled: true}, () => {
this._panel.transitionTo({toValue: height})
})
}
});
And return this error
this.requestAnimationFrame is not a function
The requestAnimationFrame method returns an integer ID which can be used to cancel the queued request using the cancelAnimationFrame(id) method. The animation callback function can also accept a timestamp value, which is the time elapsed in milliseconds since the web page was loaded.
requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. The method takes a callback as an argument to be invoked before the repaint.
requestAnimationFrame() is much more efficient than setTimeout() or setInterval() , and the result is smoother and more accurate (just check the provided demo and the consumed CPU).
The requestAnimationFrame() method tells the browser to run a callback function right before the next repaint happens. It's particularly useful when using JavaScript for animations and repeating UI updates.
You have to remove this
from the first line:
requestAnimationFrame(() => {
....
});
Update Nov 19: The previous documentation mentioned the TimerMixin
I am referring to in my answer, but it's now been removed from the documentation. So using the native browser API should be the recommended approach (which is already the accepted answer).
Actually it is recommended not to use timing functions such as requestAnimationFrame
directly in react-native. It's recommended to use TimerMixin
instead, and doing that you end up calling it like the code in the original question.
So maybe a better answer would be "you're missing the TimerMixin".
Here's the official documentation explaining this bit: https://facebook.github.io/react-native/docs/timers#timermixin
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