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
Published 6 years ago
Updated 5 years ago

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.

egghead
egghead
~ a minute ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today