简体   繁体   English

我如何第一次禁用reactjs组件中的动画

[英]How can I disable animation in a reactjs component first time

I have a component with a list of items. 我有一个带有项目列表的组件。 I want each time I add or remove an item from the list to have animation. 我希望每次我从列表中添加或删除项目时都具有动画效果。 Everything it's OK with that. 一切都很好。

My only problem is that I want to disable the animation first time when the component initialize. 我唯一的问题是,我想在组件初始化时第一次禁用动画。

http://jsfiddle.net/kb3gN/6887/ http://jsfiddle.net/kb3gN/6887/

var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup;

var Hello = React.createClass({
    getInitialState: function () {
        return {items: []};
    },
    addColor:function(){
    this.setState({items:this.state.items.concat('new color')});
    },
    componentDidMount: function () {
        var items = ['blue', 'red', 'green'];
        this.setState({items: items});
    },
    render: function () {
        var contentList = this.state.items.map(function (i,k) {
            return (<li>{i}</li>);
        });
        return (
                <div>
                Hello <button onClick={this.addColor}>add</button>
<ReactCSSTransitionGroup transitionName="example">
                {contentList}
                </ReactCSSTransitionGroup>

                </div>
        )
    }
});

React.render(<Hello name="World" />, document.body);

So, how can I make the animation appear only when changing the list and not ont the very first setState from didMount method? 因此,如何使动画仅在更改列表时出现,而不是在didMount方法的第一个setState上出现?

Using componentWillMount instead of componentDidMount it will work perfectly as shown in this fiddle : http://jsfiddle.net/ghislaindj/jpuyd3gb/1/ 使用componentWillMount而不是componentDidMount可以正常工作,如以下提琴所示: http : //jsfiddle.net/ghislaindj/jpuyd3gb/1/

However, if your real code must fetch the data in DidMount, you should use ssorallen comment. 但是,如果实际代码必须在DidMount中获取数据,则应使用ssorallen注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM