ReactJS:切换css转换的类

eye*_*mew 6 javascript css reactjs

我需要在完全呈现组件(甚至页面)之后切换css类,以便在页面加载时对相关属性进行动画处理.

我怎么做这个,最好没有jQuery?

如果我切换组件的类componentDidMount,动画实际上不会发生.

nil*_*gun 15

我没有真正得到你说的部分:

在完全呈现组件(甚至页面)之后,在页面加载时对相关属性进行动画处理.

什么时候你想要为元素设置动画?如果在render()函数中指定类名,则组件将在页面加载时使用动画进行渲染.

如果要在首次渲染后控制/切换动画,可以使用组件状态控制类名,如下所示:

var Hello = React.createClass({

    getInitialState: function(){
        return {
            condition:false
        }
    },

    handleClick :function(){
        this.setState( { condition : !this.state.condition } );
    },

    render: function() {
        return <div>
                <div className={this.state.condition ? "animated" :""}  >Hello {this.props.name}</div>
                <button type="button" onClick={this.handleClick}>Change Condition</button>

               </div>;
    }
});

React.render(<Hello name="World" />, document.body);
Run Code Online (Sandbox Code Playgroud)

在这里,我改变了状态以响应按钮点击.您可能希望将此更改为您喜欢的其他事件.

这是上面代码的小提琴:http://jsfiddle.net/f0j4kt0L/

  • 所以我想要做的是在加载页面后立即加载动画.我已经完全按照你的建议尝试了,而是改变了componentDidMount方法中的`this.state.condition`.在这种情况下,css类立即应用,没有转换. (2认同)