最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What are the callbacks in ReactTransitionGroup hooks? - Stack Overflow

programmeradmin0浏览0评论

React.js offers a low level API for animations called ReactTransitionGroup. In the docs it is stated that for the hooks ponentWillAppear, ponentWillEnter and ponentWillLeave, a callback is passed as an argument.

My question is, what exactly is this callback and how it gets passed to those hooks, the docs aren't saying anything about those callbacks except that the animation is delayed until they get called.

React.js offers a low level API for animations called ReactTransitionGroup. In the docs it is stated that for the hooks ponentWillAppear, ponentWillEnter and ponentWillLeave, a callback is passed as an argument.

My question is, what exactly is this callback and how it gets passed to those hooks, the docs aren't saying anything about those callbacks except that the animation is delayed until they get called.

Share Improve this question asked Jul 13, 2015 at 0:45 Felix D.Felix D. 2,2201 gold badge24 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

First off, I am also still learning ReactJS so there is a possibility that I could be wrong in my approach and what not. Apologies in advance for that.

Open your Developer Tools' console window and analyse this jsFiddle example that I just made. Observe the sequence with which the callbacks get called.

I am using TweenMax to do some animations to a box to make it appear or disappear upon clicking of a button.

The Low-level API exposes quite a few useful callbacks for us to utilise. Shared example demonstrates the use of these callbacks.

JavaScript:

var ReactTransitionGroup = React.addons.TransitionGroup;
var MyBox = React.createClass({
    show: function(callback){
        var node = React.findDOMNode(this);
        TweenMax.fromTo(node, 2, { width: 100, height: 100, backgroundColor: '#0cc', scale: 0.2, opacity: 0, rotation: -180 }, { width: 100, height: 100, backgroundColor: '#0cc', scale: 1, opacity: 1, rotation: 0, ease: Expo.easeInOut, onComplete: callback, onCompleteScope: this });
    },
    hide: function(callback){
        var node = React.findDOMNode(this);
        TweenMax.to(node, 2, { width: 100, height: 100, backgroundColor: '#cc0', scale: 0.2, opacity: 0, ease: Expo.easeInOut, onComplete: callback, onCompleteScope: this });
    },
    ponentWillAppear: function(didAppearCallback){
        console.log('MyBox.ponentWillAppear');
        this.show(didAppearCallback);
    },
    ponentDidAppear: function(){
        console.log('MyBox.ponentDidAppear');
    },
    ponentWillEnter: function(didEnterCallback){
        console.log('MyBox.ponentWillEnter');
        this.show(didEnterCallback);
    },
    ponentDidEnter: function(){
        console.log('MyBox.ponentDidEnter');
    },
    ponentWillLeave: function(didLeaveCallback){
        console.log('MyBox.ponentWillLeave');
        this.hide(didLeaveCallback);
    },
    ponentDidLeave: function(){
        console.log('MyBox.ponentDidLeave');
    },
    ponentDidMount: function() {
        console.log('MyBox.ponentDidMount');
    },
    ponentWillUnmount: function() {
        console.log('MyBox.ponentWillUnmount');
    },
    render: function(){
        return <div>&nbsp;</div>;
    }
});
var Container = React.createClass({
    getInitialState: function(){
        return { isShowing: false };
    },
    onButtonClicked: function(){
        this.setState({ isShowing: !this.state.isShowing });
    },
    render: function(){
        var myBox = this.state.isShowing ? <MyBox key="myBox" /> : '';
        return (
            <div id="container">
                <MyButton onButtonClicked={this.onButtonClicked} />
                <ReactTransitionGroup transitionName="hellotransition">
                    {myBox}
                </ReactTransitionGroup>
            </div>
        );
    }
});
var MyButton = React.createClass({
    render: function(){
        return <button onClick={this.props.onButtonClicked}>Click Me</button>;
    }
});
//
React.render(<Container />, document.body);

Let me know if anything is unclear and I'll be happy to share what I know.

Without the callback, React would have no way of knowing how long to wait for your custom animation. Some clarification from one of the methods from the docs:

ponentWillLeave(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.

Imagine you want to fade out a ponent when it gets mounted, and you want to implement the fade-out code in pure JavaScript. When the ponent is unmounted, React calls ponentWillLeave. You initiate the fade-out code (maybe using jQuery animations, or some tweening library), and when the fade-out is done, you call the callback to indicate that the animation is done, and then and only then will React unmount the ponent.

发布评论

评论列表(0)

  1. 暂无评论