CSS Animations
Starter Code
We'll be using this iTunes search example and add transitions to the search items. Follow the instructions to set up the project.
https://github.com/WDI-SEA/react-itunes-search
Using ReactCSSTransitionGroup
In order to implement CSS animations, we'll first need to install the appropriate React addon. We can do this via npm
.
Excellent! Now let's take a look at the documentation. https://facebook.github.io/react/docs/animation.html
You'll notice that we need to require()
the module in the file we want to use it on. Then, we can encapsulate the components we want to animate with the following component:
We also need to provide a transition name, as well as enter and leave timeouts. The timeouts will let React know when to unmount the components from the DOM.
In /src/components/MusicSearch.jsx
, let's include ReactCSSTransitionGroup at the top of the page.
Then, we'll encapsulate the {listings}
being rendered with the component we mentioned earlier, ReactCSSTransitionGroup
. The return
statement under render
should look something like this:
Finally, we have to add the actual transitions to our CSS. Add the following to sass/style.scss
.
We should now have transitions for each MusicListing
component! However, there may be an issue when searching two terms one after another. The enter transition works, but the leave transition doesn't work. We can solve this by setting the state
in the search
function, so that the array is cleared before we reassign the results.
In /src/components/MusicSearch.jsx
, make the following change to the search
function.
Now, as we make multiple searches, the results will fade in and out.
Try it: In AngularJS, we used the animate.css library for more complicated transitions. Try importing animate.css and use it with ReactCSSTransitionGroup.
Last updated