React.js offers a low level API for animations called ReactTransitionGroup. In the docs it is stated that for the hooks componentWillAppear
, componentWillEnter
and componentWillLeave
, a callback is passed as an argument.
My question is, what exactly is this callback and how it gets passed to those hooks, the docs aren't saying anything about those callbacks except that the animation is delayed until they get called.
First off, I am also still learning ReactJS so there is a possibility that I could be wrong in my approach and what not. Apologies in advance for that.
Open your Developer Tools'
console
window and analyse this jsFiddle example that I just made. Observe the sequence with which the callbacks get called.I am using
TweenMax
to do some animations to a box to make it appear or disappear upon clicking of a button.The Low-level API exposes quite a few useful callbacks for us to utilise. Shared example demonstrates the use of these callbacks.
JavaScript:
Let me know if anything is unclear and I'll be happy to share what I know.
Without the callback, React would have no way of knowing how long to wait for your custom animation. Some clarification from one of the methods from the docs:
Imagine you want to fade out a component when it gets mounted, and you want to implement the fade-out code in pure JavaScript. When the component is unmounted, React calls
componentWillLeave
. You initiate the fade-out code (maybe using jQuery animations, or some tweening library), and when the fade-out is done, you call the callback to indicate that the animation is done, and then and only then will React unmount the component.