ReactTransitionGroup
(khi đó ReactCSSTransitionGroup
được tạo) là thành phần cơ bản cho phép nhập và thoát không đồng bộ. Nó cung cấp các móc vòng đời mà bạn có thể sử dụng để móc vào các hình động dựa trên JS. The docs list móc phép:
ReactTransitionGroup
is the basis for animations. When children are declaratively added or removed from it (as in the example above) special lifecycle hooks are called on them. There are 3 ways to get starting using ReactCSSTransitionGroups
:
import ReactCSSTransitionGroup from 'react-addons-css-transition-group' // ES6
var ReactCSSTransitionGroup = require('react-addons-css-transition-group') // ES5 with npm
var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup; // ES5 with react-with-addons.js
componentWillAppear(callback)
This is called at the same time as componentDidMount()
for components that are initially mounted in a TransitionGroup
. It will block other animations from occurring until callback
is called. It is only called on the initial render of a TransitionGroup
.
componentDidAppear()
This is called after the callback
function that was passed to componentWillAppear
is called.
componentWillEnter(callback)
This is called at the same time as componentDidMount()
for components added to an existing TransitionGroup
. It will block other animations from occurring until callback
is called. It will not be called on the initial render of a TransitionGroup
.
componentDidEnter()
This is called after the callback
function that was passed to componentWillEnter
is called.
componentWillLeave(callback)
This is called when the child has been removed from the ReactTransitionGroup
. Though the child has been removed, ReactTransitionGroup
will keep it in the DOM until callback
is called.
componentDidLeave()
This is called when the willLeave
callback
is called (at the same time as componentWillUnmount
).
Animation - Low-level API
Để animate một đứa trẻ ra ngoài, bạn cần để bắt đầu hoạt hình của bạn trong componentWillLeave
và gọi được cung cấp callback
khi hoạt ảnh hoàn tất. Như một ví dụ, here's a JSFiddle cho thấy một thành phần tách rời-sinh động trẻ em của mình trong và ngoài: http://jsfiddle.net/BinaryMuse/f51jbw2k/
Mã liên quan để tạo hiệu ứng động là:
componentWillLeave: function(callback) {
this._animateOut(callback);
},
_animateOut(callback) {
var el = ReactDOM.findDOMNode(this);
setTimeout(function() {
TweenLite.to(el, 1, {opacity: 0}).play().eventCallback("onComplete", callback);
}, this.props.animateOutDelay);
},
Bạn nên làm điều đó trong hai bước mà một thành phần để lại được đánh dấu bằng một số trạng thái cục bộ trong thành phần, kích hoạt hoạt ảnh. Khi hoạt ảnh hoàn tất, nó có thể thông báo cho phụ huynh xóa hoàn toàn. – WiredPrairie