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
- Animation Types: Understand "appear," "enter," and "leave" transitions.
- Dynamic Rendering: Use state and array manipulation to manage child components dynamically.
- 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.
Video Transcript
00:00We already met one animation type
00:03with the React CSS transition group.
00:05And as we get closer to the end of this title, it's time
00:08for us to reveal the next three, well actually the next two
00:12and to together three types of animation
00:14that you could create with the React transition group.
00:16And the next one is gonna be the enter,
00:18which is gonna be used whenever we add things onto the
00:21stage, contrary to when we're adding the whole component in.
00:24So let's see it in action.
00:26All righty, ladies and gentlemen, we're ready
00:29for the last lecture and it's time for us to start animating
00:32or return to animating.
00:33Now to do that, I'm gonna go ahead
00:35and I want to add a bit more logic into our app.
00:38And by the way, one thing I want to fix
00:39before we continue is I really don't like it
00:42that our initial state there has nothing.
00:43And that is because if a property doesn't have a value
00:46and zero is considered no value,
00:48it actually comes back as emptiness.
00:50And emptiness is horrid in our situation.
00:53So instead of that, I could go ahead
00:55and send it in as that body
00:57because we already changed our button to be able to accept
01:01or a label or content.
01:03And in this case, now that it could accept either or
01:06whenever we're gonna send it as that body element.
01:08And our item here refreshes, uh, by the way, you ignore this
01:12'cause we're gonna fix it in a second.
01:14Um, why aren't you refreshing? Did I not save you?
01:19Really? You're doing that to me right now.
01:21Now you're gonna crash after all this
01:22work we've been doing together.
01:25Silly computer. Alright,
01:27I'm just gonna force a refresh and wait.
01:31There you go. Okay, so we got ourselves our zero. Beautiful.
01:34So let's continue now into the actual
01:36steps of our animation.
01:37And let's also just sort out these errors that we see
01:40or that are not really errors
01:41or more warnings that are telling us that
01:44whenever we're doing animations we have
01:46to define their times.
01:47Now there are three types of animations.
01:50We met the peer and in this lecture we're gonna
01:52meet two other ones.
01:53But I'm gonna just go ahead here
01:55because we were really not using this.
01:57I'm just gonna duplicate them.
01:58And I have your enter
02:01and I have less, which is exit, which is called,
02:06which is called leave.
02:07Okay? So we have your end an appear,
02:09which is when the whole component appears
02:12for the first time when it's mounted, while our
02:16enter is when new information enters into the component.
02:19And our transition leave is when our actual
02:22component leaves us.
02:23So I'm just gonna go ahead and save this and let's go ahead
02:27and what I'm gonna do is I'm just gonna copy
02:30that whole react transition group.
02:33'cause what I really want to do is I wanna basically create
02:37visual changes that are deeper than just
02:39that button being changed or modified.
02:42So what I'm gonna do is I'm gonna go into our states
02:46and I wanna go ahead and I'm gonna add here a new state
02:49and, oh, oh, sorry not, I'm not gonna call it state.
02:51I'm gonna call it labels. And
02:52what I'm gonna do is every time a click event happens,
02:55I'm gonna go ahead and add something into that state.
02:59Now to do that, what I'm gonna do once I've updated
03:02and set that LA that that labels value,
03:05I'm gonna go ahead here and create a new
03:08variable which I'll call labels or a new let.
03:11And I'm gonna go ahead and take that state and labels
03:14and I'm gonna go ahead and concat
03:16'cause I need to create a new array
03:18and I'm gonna concat you at another array.
03:20But this time around, I'm just gonna go ahead
03:22and just take the actual click state
03:24that was just before that.
03:25Click, click, okay.
03:27So right now all I'm doing here is just creating a new label
03:30that is just gonna be a a number just
03:32for the sake of our sample.
03:34Last but not least, I'm also gonna go ahead into our state
03:37and update it by sending in to its labels parameter,
03:41the new labels value.
03:43Okay, so now that we're gonna have a state that's changing
03:45and we're gonna have some sort of a labels construct
03:48that we'll be able to approach, the next step for us is
03:50to go ahead and actually add that into our rendering logic.
03:55So for to our L rendering logic, I'm, I need
03:58to create these children dynamically.
04:00Now to do that, what I'm gonna go ahead,
04:02I'm gonna create here a new variable.
04:04I'm gonna call it buttons.
04:06And I'm gonna literally create here a pile of buttons
04:08and they're gonna be derived out of the state labels.
04:11And again, this is just for the sake of sampling
04:14and I'm gonna use a new ES five method, which is called map,
04:18which enables us to run through everything inside
04:21of an array and return those items.
04:23Basically we're gonna be returning
04:26the values into our buttons.
04:28So in our map, I'm gonna go ahead
04:30and put here something which I don't remember if we learned
04:32about it or not, but if we haven't then hey,
04:35we're gonna learn about it right now,
04:36which is called Arrow functions.
04:38Now, arrow functions on contrary to our on click,
04:42which was a regular function, its scope disappeared.
04:45So we had to bind that scope
04:46whenever we were passing it around.
04:48Whenever we're using Arrow functions,
04:50which is this structure, then the, this stays the this
04:53of our, of our component that we're within.
04:56Now our map method expects this function
04:58that we're sending in to have an item and the index
05:02or more accurately the value
05:05and the actual index that it's at.
05:08Okay? So now that we have that information, let's go ahead
05:11and build out a button.
05:12And to do that I'm gonna go ahead
05:13and I'm gonna return a new button
05:16and I'm gonna make sure that I'm gonna open
05:18and close that button and I'm gonna make sure
05:22that I'm gonna send the value to be the actual value
05:25that is being sent through and in for our button itself.
05:29I'm gonna go ahead. Oh, last thing really critical.
05:32Whenever we're putting dynamic content into React, we have
05:35to set a key to that value.
05:38Whenever we're not putting that content just
05:40as hard coded JSX
05:42and we're actually gonna embed it as extra children
05:45and we're creating it dynamically, we need
05:47to explicitly give it a key.
05:49And that key needs to be unique for this rendering session.
05:52It doesn't have to be unique for the whole React.
05:54So for this case, we could literally just use that
05:56Index because our index itself will be unique.
06:00And now that we have a unique id, we're ready
06:02to actually take all these buttons that we've just created.
06:05Literally all we're doing here is running
06:07through all the labels that exist
06:09and where we're, we're setting them a value
06:12and we're setting setting for them.
06:13And index will, in this case, the index and the click.
06:16The values are gonna be very similar.
06:18If we wanted to, we could just put here the value
06:20and correspondent, which would make sense if at any stage we
06:24wanted to remove a button
06:25because if we associated it with the index of that array,
06:28that index might change if we remove an item, right?
06:32Okay. So with that said, once we have a key,
06:34we have a value, we have a button, it's time to us to inject
06:37that button into somewhere.
06:39And I'm gonna just go ahead
06:40and literally just place it right inside here.
06:44Now, if I go ahead into my browser
06:46and once it's actually done refreshing, you'll see
06:49that every time I click on we're gonna to get another button
06:52that is being added in.
06:54The colors are not really the greatest
06:55colors, but that's fine.
06:57Okay, so let's go ahead and now add the animation in
07:00and to do that, because it's a deeper child,
07:03all these buttons, they're not a direct child of
07:05that react transition group.
07:07I'm gonna need to use another react transition group.
07:10So I'm gonna go ahead here
07:11and just copy this react transition group
07:14and I'm literally gonna wrap it with our buttons.
07:17So our buttons are gonna live within
07:19that React transition group.
07:21Now I don't need that transition to appear anymore
07:24because we're actually not gonna use it.
07:25And that's all I need to do from our actual JSX part
07:29to get our buttons to animate.
07:31But we also need to basically add in the actual CSS
07:35of the enter timeout and leave timeout.
07:37Okay? So we're gonna go ahead and do that right now.
07:40Alright, let's go into our CSS.
07:42Let's learn a little bit more about SAS
07:44before we wrap things up.
07:46And what I'm gonna do is I'm gonna grab this appear me, um,
07:49classes and I'm gonna go ahead and set them OTO for enter.
07:53Now this time around, because our enter is actually gonna
07:56have five seconds there, I'm gonna oto change
07:58that from 500 milliseconds into five seconds.
08:02Now if I go ahead and save this
08:03and just check my application,
08:04everything should be working at this stage.
08:08So if I just go ahead and,
08:09and just click your refresh just
08:10so we could see the fade in.
08:11We have our quick fade in
08:13and if we click on our item, we'll see
08:15that it's gonna fade in really slowly.
08:17It's gonna take it about five seconds for the animation
08:19to come all the way in.
08:22Now before we wrap up this topic, I want to, in the next,
08:25I'm gonna split this into another lecture.
08:27I wanna talk about how
08:28to build out your SaaS a little bit better
08:32and learn about SaaS nesting
08:34as we OSU complete our OSU transitioning out.
08:37Okay? So let's do that in the next lecture.
08:40In this lecture we explored the second type of animation
08:43with the React Transition group.
08:45And in the last lecture in this title,
08:47we're gonna continue animations as we explore the leaving
08:51or the leave animation type.
08:53And also learn a little bit more about CSS
08:56and SaaS as we're gonna explore SaaS nesting.
