admin管理员组文章数量:1354745
I am new to react native and so this question might seem trivial question. It has got more to do with passing functions as parameters and I am confused about the fat arrow function vs passing a function as direct parameter. I am using Animated.View for my animation and I wrote a generic function for animating values as follows:
animateToFadeState(stateName, newStateValue, onAnimEndCallback) {
const hasCallback = onAnimEndCallback !== undefined;
Animated.timing(stateName, {
toValue: newStateValue,
duration: 3000
}).start(
hasCallback ? this.onAnimEndCallback() : null;
);
}
Here you can pass the stateName to animate and newState and with optional callback. The problem I am facing is how to pass callback as it requires a fat arrow function to be passed as parameter.
I think there are multiple ways to call this function (all differing in the way callback function is passed).
- animateToFadeState(this.state.abc, 1, () => {this.myFunction()})
- animateToFadeState(this.state.abc, 1, this.myFunction())
- animateToFadeState(this.state.abc, 1, () => this.myFunction())
- animateToFadeState(this.state.abc, 1, this.myFunction)
None of these seem to be working, as my callback is getting called immediately when the animation starts. I don't quite understand what is wrong here and am not sure of the difference between passing function vs fat arrow function.
I am new to react native and so this question might seem trivial question. It has got more to do with passing functions as parameters and I am confused about the fat arrow function vs passing a function as direct parameter. I am using Animated.View for my animation and I wrote a generic function for animating values as follows:
animateToFadeState(stateName, newStateValue, onAnimEndCallback) {
const hasCallback = onAnimEndCallback !== undefined;
Animated.timing(stateName, {
toValue: newStateValue,
duration: 3000
}).start(
hasCallback ? this.onAnimEndCallback() : null;
);
}
Here you can pass the stateName to animate and newState and with optional callback. The problem I am facing is how to pass callback as it requires a fat arrow function to be passed as parameter.
I think there are multiple ways to call this function (all differing in the way callback function is passed).
- animateToFadeState(this.state.abc, 1, () => {this.myFunction()})
- animateToFadeState(this.state.abc, 1, this.myFunction())
- animateToFadeState(this.state.abc, 1, () => this.myFunction())
- animateToFadeState(this.state.abc, 1, this.myFunction)
None of these seem to be working, as my callback is getting called immediately when the animation starts. I don't quite understand what is wrong here and am not sure of the difference between passing function vs fat arrow function.
Share Improve this question asked Nov 21, 2017 at 16:33 RoadblockRoadblock 2,0712 gold badges26 silver badges38 bronze badges3 Answers
Reset to default 4I am using Animated like this:
The callback is inside the start()
parameter with an arrow function
Animated.timing(this.state.xValue, {
toValue: footerHeightPosition,
duration: MILLISECOND_ANIMATION_DURATION,
asing: Easing.back(),
}).start(() => {
//
this.youCallbackFunc();
});```
Assuming onAnimEndCallback
does not return a function you should change your code and pass the function reference rather than the result of its invocation.
Another thing to note, there's no need to add 'this' keyword (this.onAnimEndCallback) since onAnimEndCallback is just a parameter and not a field of your context.
Something like this should work
animateToFadeState(stateName, newStateValue, onAnimEndCallback) {
const hasCallback = (typeof onAnimEndCallback === "function");
//invoke Animated.timing only if you have a valid function
if(hasCallback){
Animated.timing(stateName, {
toValue: newStateValue,
duration: 3000
}).start(onAnimEndCallback);
);
}
}
animateToFadeState(this.state.abc, 1, () => { //do something })
Building on top of answer Karim's, I just added fat arrow function on both ends and the function in callback gets called after the animation ends.
animateToFadeState(stateName, newStateValue, onAnimEndCallback) {
const hasCallback = (typeof onAnimEndCallback === "function");
//invoke Animated.timing only if you have a valid function
if(hasCallback){
Animated.timing(stateName, {
toValue: newStateValue,
duration: 3000
}).start(() => {onAnimEndCallback});
);
}
}
and calling it as follows:
animateToFadeState(this.state.abc, 1, () => { //do something })
本文标签: javascriptReact Native Animation CallbackStack Overflow
版权声明:本文标题:javascript - React Native Animation Callback - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744030884a2578881.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论