Show React Transition Group Elements with an Initial Transition with the `appear` Prop

Rory Smith
InstructorRory Smith
Share this video with your friends

Social Share Links

Send Tweet

In React Transition Group, it can be a common situation for a transition to appear when the component renders, rather than wait for any state to change based on, for example, a button click. Should we want the component to show on mounting, instead of waiting for any state to change, we can make use of the appear prop, which adds two targetable classnames; *-appear and *-appear-active.

Instructor: [00:00] Here, we have a button which toggles a menu component, and this is controlled with a CSS transition which is using a balloon transition, controlled by the enter, enter-active, exit, and exit-active selectors in the CSS.

[00:21] Let's say we wanted this menu to show initially when the page loads, so before the user clicks the button. We can just change the stateful show-balloon value to true. Then when we refresh the page, the menu shows instantly, but there's no transition on it.

[00:45] We can achieve a transition initially with the appear prop. In the CSS transition component, we can pass appear as true. What that's going to do is add an extra pair of class names onto this menu on the initial load.

[01:04] Then we can head over to our CSS, and include balloon-appear and balloon-appear-active, which will share the same styles as enter and enter-active. Now, when the page loads, we get the initial transition.