react-transition-group 使用方法

初始化渲染 Transition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
render() {
return (
<CSSTransitionGroup
transitionName="example"
transitionAppear={true}
transitionAppearTimeout={500}
transitionEnter={false}
transitionLeave={false}>

<h1>Fading at Initial Mount</h1>
</CSSTransitionGroup>

)
}

// css
.example-appear {
opacity: 0.01;
}

.example-appear.example-appear-active {
opacity: 1;
transition: opacity .5s ease-in;
}

必须设置 transitionName 属性,用于相应名称的 css 选择器
transitionAppear 默认值为 false,如需组件一加载就 transition,则设置为 true
在这里如果不需要 transitionEnter,transitionLeave,则需设置为 false(默认为 true)

动态增加、删除及显示隐藏 Transition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
render() {
return (
<CSSTransitionGroup
transitionName="example"
transitionEnterTimeout={500}
transitionLeaveTimeout={500}
>

{ this.state.show &&
<div>toggled element</div>
}
</CSSTransitionGroup>


}

// css
.example-enter {
opacity: 0.01;
}

.example-enter.example-enter-active {
opacity: 1;
transition: opacity 500ms ease-in;
}

.example-leave {
opacity: 1;
}

.example-leave.example-leave-active {
opacity: 0.01;
transition: opacity 300ms ease-in;
}

官方文档