02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Animating Children with ReactCSSTransitionGroup

In this tutorial, we’ll learn how to animate child components dynamically using ReactCSSTransitionGroup, manage transitions effectively, and enhance styles with Sass.


1. Animation Types in React Transition Group

ReactCSSTransitionGroup supports three types of animations:

  • Appear: Used when the component is mounted.
  • Enter: Triggered when new items are added dynamically.
  • Leave: Triggered when items are removed.

2. Setting Up "Enter" and "Leave" Transitions

Add "enter" and "leave" timeout properties in your component:

<ReactCSSTransitionGroup
    transitionName="example"
    transitionAppear={true}
    transitionAppearTimeout={500}
    transitionEnterTimeout={500}
    transitionLeaveTimeout={500}>
    {this.state.items.map(item => (
        <div key={item.id}>{item.text}</div>
    ))}
</ReactCSSTransitionGroup>

3. Dynamic Child Components with State

Create a dynamic list of child components that respond to state changes:

handleAdd = () => {
    const newItem = { id: Date.now(), text: `Item ${this.state.items.length + 1}` };
    this.setState(prevState => ({ items: [...prevState.items, newItem] }));
};

handleRemove = id => {
    this.setState(prevState => ({
        items: prevState.items.filter(item => item.id !== id)
    }));
};

4. Adding CSS for Animations

Define CSS rules for "enter," "leave," and "appear" transitions:

.example-enter {
    opacity: 0;
    transform: scale(0.9);
}
.example-enter-active {
    opacity: 1;
    transform: scale(1);
    transition: opacity 500ms, transform 500ms;
}
.example-leave {
    opacity: 1;
    transform: scale(1);
}
.example-leave-active {
    opacity: 0;
    transform: scale(0.9);
    transition: opacity 500ms, transform 500ms;
}

5. Sass Nesting for Cleaner Code

Leverage Sass nesting to structure your styles:

.example {
    &-enter {
        opacity: 0;
        transform: scale(0.9);
    }
    &-enter-active {
        opacity: 1;
        transform: scale(1);
        transition: opacity 500ms, transform 500ms;
    }
    &-leave {
        opacity: 1;
        transform: scale(1);
    }
    &-leave-active {
        opacity: 0;
        transform: scale(0.9);
        transition: opacity 500ms, transform 500ms;
    }
}

Key Takeaways

  1. Animation Types: Understand "appear," "enter," and "leave" transitions.
  2. Dynamic Rendering: Use state and array manipulation to manage child components dynamically.
  3. CSS Best Practices: Utilize Sass nesting to maintain clean, organized styles.

In the next tutorial, we’ll continue exploring animations by learning how to manage leaving components and delve deeper into Sass features like nesting.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!

Animating in React with Addons

Learn how to animate React components using the CSS Transition Group module.

06:40

Choosing Between Children or Props in React

Learn how to dynamically manage React components with props and children. Prepare for React state and animations.

03:33

Working with Events in React

Learn how to handle events in React with ES6, manage scope, and bind "this" for optimal component functionality.

05:00

Understanding How State Works

Learn the fundamentals of state in React and how it enables dynamic, self-aware components. Prepare for state-driven animations in upcoming lessons.

03:43

Animating Children with ReactCSSTransitionGroup

Learn how to animate child components dynamically with ReactCSSTransitionGroup, including enter and leave transitions. Refine styles with Sass nesting for a polished UI.

09:00

Leaving Animations with ReactCSSTransitionGroup and Sass Nesting

Master "leave" animations in ReactCSSTransitionGroup and optimize your workflow with Sass nesting. Create polished exit transitions and dynamic UI elements.

07:29