反应“反应过渡组”不起作用

时间:2017-05-09 12:00:06

标签: javascript css facebook reactjs animation

我正在将我的项目迁移到React的最新版本,我看到ReactCSSTransitionGroupdeprecated ...所以我使用了React devs的recomended package。当我使用ReactCSSTransitionGroup时,动画在组件渲染时可以正常工作,但是使用此包时,组件不起作用并且不会设置动画。

我做了一个简单的例子来测试,但不起作用......怎么回事?

反应代码:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import CSSTransitionGroup from 'react-transition-group/CSSTransitionGroup';

class App extends Component {
    render() {
        return (
            <CSSTransitionGroup
              transitionName="example"
              transitionAppear={true}
              transitionAppearTimeout={3000}
              transitionEnter={true}
              transitionEnterTimeout={3000}
              transitionLeave={false}>

                <div id="container">
                    Animation test
                </div>

            </CSSTransitionGroup>
        );
    }
}

ReactDOM.render(
    <App />,
    document.getElementById('app')
);

CSS代码:

.example-enter {
    opacity: 0.01;
    border: 1px solid red;
}
.example-enter.example-enter-active {
    opacity: 1;
    transition: all 2s ease 0s;
    border: 1px solid orange;
}
.example-leave {
    opacity: 1;
    border: 1px solid green;
}
.example-leave.example-leave-active {
    opacity: 0.01;
    transition: all 3s ease 0s;
    border: 1px solid blue;
}

1 个答案:

答案 0 :(得分:3)

来自doc:&#34;您必须为CSSTransitionGroup的所有子项提供key属性,即使只渲染单个项目也是如此。这就是React将如何确定哪些孩子已进入,离开或停留。&#34;

此外,您还应该检查您的css类名称并添加&#34;出现&#34;如果在初始安装期间需要动画,则为前缀:

.example-appear {
    opacity: 0.01;
    border: 1px solid red;
}
.example-appear.example-appear-active {
    opacity: 1;
    transition: all 2s ease 0s;
    border: 1px solid orange;
}