SlideShare a Scribd company logo
React Animations
Two Types
Component In the DOM
Component Entering/Leaving the DOM
Add a class or
whatever
Two Types
Component In the DOM
Component Entering/Leaving the DOM
Add a class or
whatever
Review: React Lifecycle
DOM
Virtual DOM
"PotatoHead": {
"head": {
"peg": null
},
"body": {
"topPeg": "eyes",
"middlePeg": null,
"bottomPeg": null
},
"bottom": {
"peg": "shoes"
}
}
dispatch(existentialCrisis())
Virtual DOM
"PotatoHead": {
"head": {
"peg": null
},
"body": {
"topPeg": "eyes",
"middlePeg": null,
"bottomPeg": "mouth"
},
"bottom": {
"peg": "shoes"
}
}
DOM
Animations don’t
really fit. . .
Lifecycle
componentWillMount
Render!
componentDidMount
componentWillMount
componentDidMount
More Lifecycle Hooks
componentWillMount
Render!
componentDidMount
More Lifecycle Hooks
ReactTransitionGroups
ReactTransitionGroups
Render!
componentWillMount
componentDidMount
More Lifecycle Hooks
ReactTransitionGroups
ReactTransitionGroups
Render!
componentWillMount
componentDidMountcomponentWillEnter
ReactTransitionGroups
class Box extends React.Component {
componentWillEnter (done) {
const el = findDOMNode(this);
TweenMax.fromTo(el, 0.3, {y: 100},
{y: 0, onComplete: done}
);
}
componentWillLeave (done) {
const el = findDOMNode(this);
TweenMax.fromTo(el, 0.3, {y: 0},
{y: -100, onComplete: done}
);
}
render () {
return <div className="box"/>;
}
}
Source: “Animations with ReactTransitionGroup” by Chang Wang on Medium
ReactCSSTransitionGroups
A simple abstraction that handles many use cases.
Implementation in code at react/src/addons/transitions
ReactCSSTransitionGroups
const ShapeContainer = ({elements}) => (
<div id = "shapes">
<ReactCSSTransitionGroup
transitionName = "shape"
transitionEnterTimeout={2000}
transitionLeaveTimeout={2000}
>
{elements}
</ReactCSSTransitionGroup>
</div>
)
ReactCSSTransitionGroups
.shape-enter {
transform: scale(0);
}
.shape-enter.shape-enter-active {
transform: scale(1);
transition: all 2s ease-in;
}
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
</span>
</div>
Created by ReactCSSTransitionGroup
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
class="shape-enter">
<circle data-reactid=".0.2"></circle>
</svg>
</span>
</div>
Scale to 0
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
class="shape-enter shape-enter-active">
<circle data-reactid=".0.2"></circle>
</svg>
</span>
</div>
Transition Triggered: Scaling to 1
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
class="">
<circle data-reactid=".0.2"></circle>
</svg>
</span>
</div>
Transition Complete
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
…
</svg>
</span>
</div>
Scale to 0
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
…
</svg>
<svg data-reactid=".0.2"
class="shape-enter">
<circle data-reactid=".0.2"></circle>
</svg>
</span>
</div>
Scale to 0
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
…
</svg>
<svg data-reactid=".0.2"
class=“shape-enter shape-enter-active“>
<circle data-reactid=".0.2"></circle>
</svg>
</span>
</div>
Scaling to 1
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
…
</svg>
<svg data-reactid=".0.2"
class="">
<circle data-reactid=".0.2"></circle>
</svg>
</span>
</div>
Transition Complete
Leaving is the same
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
…
</svg>
<svg data-reactid=".0.2"
class="">
<circle data-reactid=".0.2"></circle>
</svg>
</span>
</div>
ReactCSSTransitionGroups
.shape-leave {
transform: scale(1);
}
.shape-leave.shape-leave-active {
transform: scale(0);
transition: all .5s ease-in;
}
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
…
</svg>
<svg data-reactid=".0.2"
class="shape-leave">
<circle data-reactid=".0.2"></circle>
</svg>
</span>
</div>
Scale to 1
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
…
</svg>
<svg data-reactid=".0.2"
class=“shape-leave shape-leave-active“>
<circle data-reactid=".0.2"></circle>
</svg>
</span>
</div>
Scaling to 0
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
…
</svg>
<svg data-reactid=".0.2"
class="">
<circle data-reactid=".0.2"></circle>
</svg>
</span>
</div>
Transition Complete
ReactCSSTransitionGroups
<div id="shapes">
<span data-reactid="0.1">
<svg data-reactid=".0.2"
…
</svg>
</span>
</div>
Element Disappears
Demo
http://codepen.io/jsmapr1/pen/KgdNWy
Takeaways
1. Animations in React rely on new lifecycle hooks
2. ReactTransitions give more control and allow callbacks
when animations end
3. ReactCSSTransitions allow a simple hook for CSS-based
transitions
Thanks
blog: thejoemorgan.com
twitter: joesmorgan
github: jsmapr1

More Related Content

PDF
Secrets of JavaScript Libraries
PPT
JavaScript Needn't Hurt!
ZIP
Fundamental JavaScript [In Control 2009]
ODP
ActiveWeb: Chicago Java User Group Presentation
PDF
ActiveJDBC - ActiveRecord implementation in Java
KEY
JavaScript Neednt Hurt - JavaBin talk
PDF
jQuery Features to Avoid
PDF
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Secrets of JavaScript Libraries
JavaScript Needn't Hurt!
Fundamental JavaScript [In Control 2009]
ActiveWeb: Chicago Java User Group Presentation
ActiveJDBC - ActiveRecord implementation in Java
JavaScript Neednt Hurt - JavaBin talk
jQuery Features to Avoid
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K

What's hot (10)

PPTX
PPTX
Unobtrusive javascript with jQuery
PPTX
Maintainable JavaScript 2012
PDF
JavaScript - From Birth To Closure
PDF
Patterns for JVM languages - Geecon 2014
ZIP
Solving Problems with YUI3: AutoComplete
ODP
Step objects
PDF
Javascript in Plone
PPTX
Scriptaculous
PPTX
End-to-end testing with geb
Unobtrusive javascript with jQuery
Maintainable JavaScript 2012
JavaScript - From Birth To Closure
Patterns for JVM languages - Geecon 2014
Solving Problems with YUI3: AutoComplete
Step objects
Javascript in Plone
Scriptaculous
End-to-end testing with geb
Ad

Similar to React Animations (20)

PDF
Dagger 2 vs koin
PDF
Griffon @ Svwjug
PPTX
Python Code Camp for Professionals 3/4
PDF
Clickable DIVs and other icebergs
KEY
An in-depth look at jQuery
PDF
Angular JS2 Training Session #1
PDF
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
PDF
Poniendo Kotlin en producción a palos (Kotlin in production, the hard way)
PDF
Dependency Injection
PDF
From render() to DOM
PPTX
React outbox
PDF
Perkenalan ReasonML
PDF
Idioms in swift 2016 05c
PDF
Reviewing OOP Design patterns
PDF
Getting Started With Material Design
PDF
JavaScript Essentials in 1 Hour (2018)
PDF
Kotlin Overview (PT-BR)
PDF
Reactive Type-safe WebComponents
PPT
SystemVerilog OOP Ovm Features Summary
PDF
JQuery in Seaside
Dagger 2 vs koin
Griffon @ Svwjug
Python Code Camp for Professionals 3/4
Clickable DIVs and other icebergs
An in-depth look at jQuery
Angular JS2 Training Session #1
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Poniendo Kotlin en producción a palos (Kotlin in production, the hard way)
Dependency Injection
From render() to DOM
React outbox
Perkenalan ReasonML
Idioms in swift 2016 05c
Reviewing OOP Design patterns
Getting Started With Material Design
JavaScript Essentials in 1 Hour (2018)
Kotlin Overview (PT-BR)
Reactive Type-safe WebComponents
SystemVerilog OOP Ovm Features Summary
JQuery in Seaside
Ad

More from Joe Morgan (7)

PDF
Unbreakable: The Craft of Code
PDF
ES6 patterns in the wild
PDF
Git the easy way
PPTX
Reference Interviews: Where the fun never ends
PPT
Statement of Teaching Philosophy
PPT
Option 2
PPTX
Library Committee Meeting
Unbreakable: The Craft of Code
ES6 patterns in the wild
Git the easy way
Reference Interviews: Where the fun never ends
Statement of Teaching Philosophy
Option 2
Library Committee Meeting

Recently uploaded (20)

PPTX
ai tools demonstartion for schools and inter college
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Understanding Forklifts - TECH EHS Solution
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
top salesforce developer skills in 2025.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Digital Strategies for Manufacturing Companies
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
AI in Product Development-omnex systems
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Transform Your Business with a Software ERP System
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
ai tools demonstartion for schools and inter college
Which alternative to Crystal Reports is best for small or large businesses.pdf
Odoo POS Development Services by CandidRoot Solutions
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Navsoft: AI-Powered Business Solutions & Custom Software Development
Understanding Forklifts - TECH EHS Solution
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
top salesforce developer skills in 2025.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Digital Strategies for Manufacturing Companies
Design an Analysis of Algorithms I-SECS-1021-03
AI in Product Development-omnex systems
How to Migrate SBCGlobal Email to Yahoo Easily
How to Choose the Right IT Partner for Your Business in Malaysia
Transform Your Business with a Software ERP System
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Operating system designcfffgfgggggggvggggggggg
ManageIQ - Sprint 268 Review - Slide Deck
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx

React Animations