
props 接收父组件传递过来的数据this.state 初始化内部的数据bind 为事件绑定实例 (this)网络请求 就是最好的地方(官方建议)componentWillUnmount 取消订阅)timer,取消网络请求或清除componentDidMount() 中创建的订阅等首先来看 CSSTransition,从 CSSTransition 状态开始介绍,CSSTransition 有三个状态:
当组件 第一次加载,当组件 显示,当组件 退出,的时候会自动查找如下类名:
npm install react-transition-group --saveimport {CSSTransition} from 'react-transition-group';.-enter / .-enter-active / .-enter-done给 CSSTransition 添加一些属性:
App.js:
import React from 'react';
import './App.css'
import {CSSTransition} from 'react-transition-group';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
isShow: false
}
}
render() {
return (
<div>
<CSSTransition in={this.state.isShow}
classNames={'box'}
timeout={3000}>
<div/>
</CSSTransition>
<button onClick={() => {
this.btnClick()
}}>显示
</button>
</div>
);
}
btnClick() {
this.setState({
isShow: true
})
}
}
export default App;App.css:
.box-enter {
/*
进入动画执行之前绑定的类名
*/
width: 0;
height: 0;
opacity: 0;
background: skyblue;
}
.box-enter-active {
/*
进入动画执行过程中绑定的类名
*/
width: 100px;
height: 100px;
opacity: 1;
transition: all 3s;
}
.box-enter-done {
/*
进入动画执行完毕之后绑定的类名
*/
width: 100px;
height: 100px;
opacity: 1;
background: red;
}退出状态的类名的情况, 修改 App.css 添加如下类名样式:
.box-exit {
/*
退出动画执行之前绑定的类名
*/
width: 100px;
height: 100px;
opacity: 1;
background: red;
}
.box-exit-active {
/*
退出动画执行过程中绑定的类名
*/
width: 0;
height: 0;
opacity: 0;
transition: all 3s;
}
.box-exit-done {
/*
退出动画执行完毕之后绑定的类名
*/
width: 0;
height: 0;
opacity: 0;
background: skyblue;
}App.js:
import React from 'react';
import './App.css'
import {CSSTransition} from 'react-transition-group';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
isShow: false
}
}
render() {
return (
<div>
<CSSTransition in={this.state.isShow}
classNames={'box'}
timeout={3000}>
<div/>
</CSSTransition>
<button onClick={() => {
this.setState({
isShow: true
})
}}>显示
</button>
<button onClick={() => {
this.setState({
isShow: false
})
}}>隐藏
</button>
</div>
);
}
}
export default App;如上的退出状态结束之后元素并没有删除,只是进行了隐藏,如果想,在退出状态结束之后将元素进行删除的话需要借助一个 unmountOnExit 进行实现。

第一次加载时的状态,就是在页面刚加载的时候触发的,修改 App.css 添加第一次加载的类名:
.box-appear {
/*
初始化动画执行之前绑定的类名
*/
width: 0;
height: 0;
opacity: 0;
background: skyblue;
}
.box-appear-active {
/*
初始化动画执行过程中绑定的类名
*/
width: 100px;
height: 100px;
opacity: 1;
transition: all 3s;
}
.box-appear-done {
/*
初始化动画执行完毕之后绑定的类名
*/
width: 100px;
height: 100px;
opacity: 1;
background: red;
}修改 App.js 给 CSSTransition 添加 appear 属性:
import React from 'react';
import './App.css'
import {CSSTransition} from 'react-transition-group';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
isShow: true
}
}
render() {
return (
<div>
<CSSTransition in={this.state.isShow}
classNames={'box'}
timeout={3000}
unmountOnExit={true}
appear
>
<div/>
</CSSTransition>
<button onClick={() => {
this.setState({
isShow: true
})
}}>显示
</button>
<button onClick={() => {
this.setState({
isShow: false
})
}}>隐藏
</button>
</div>
);
}
}
export default App;
本期结束咱们下次再见👋~
🌊 关注我不迷路,如果本篇文章对你有所帮助,或者你有什么疑问,欢迎在评论区留言,我一般看到都会回复的。大家点赞支持一下哟~ 💗

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。