๐ฝ๏ธReact Transition Group
A third party library to overcome basic animation limitations in React.
Installation
Transition
The Transition
component lets us describe a transition from one component state to another "over time" with a simple declarative API. Most commonly it is used to animate the mounting
and unmounting
of a component, but it can be used to describe in-place transitions as well.
Transition States
By default, the Transition component does not alter the behavior of the component it renders, it only tracks the "ENTRY" & "EXIT" state of the components. There are 4 main states that a Transition can be in:
entering
entered
exiting
exited
The Transition
state is toggled via the in
prop. When true
(on button click), the component begins the "enter" stage. During this stage, the component is in the entering
state for the duration of the transition, i.e, 500ms, as defined in the timeout
prop. It then moves to the entered
state once the duration is completed.
When the in
prop is false
, the same thing happens, except the state moves from exiting
to exited
in the "exit" stage.
The animation timing prop, timeout
, can be configured according to our needs:
We can use these states, along with Transition
props like mountOnEnter
, unmountOnExit
to add or remove the component from the DOM, respectively.
Sometimes we want to execute some code when the state of the animation finishes, and not just change what renders on the screen. For this, we have various callbacks we can add to function props and execute in a Transition
component, which will run on various states of the animation.
All these Transition properties can be useful for staggered animations, where we would wait for one animation to finish before we can run another animation, and execute some code in between these animations.
CSSTransition
applies a pair of class names during the appear
, enter
, and exit
states of the transition. The first class is applied and then a second *-active
class in order to activate the CSS transition. After the transition, matching *-done
class names are applied to persist the transition state.
So instead of manually adding CSS classes to our component when the Transition is entering
or exiting
, we can make use of CSSTransition
component, with the classNames
property. This property takes in an object, if you want to give alias to your classes or if you are using CSS Modules. If you're using global CSS styles, it just takes in the base class name, and attaches the postfix classes by itself. The object keys are the keywords in the react-transition-group
package, and should be the same.
Using CSSTransition
CSSTransition
Last updated