react-transition-group 包含:
CSSTransition 基于 Transition 组件构建
CSSTransition 执行过程中,有三个状态:appear
、enter
、exit
它们有三种状态,需要定义对应的 CSS 样式
-apper
,-enter
,exit
-appear-active
,-enter-active
,-exit-active
-appear-done
,-enter-done
,-exit-done
常见属性
钩子函数
基本使用
import React, { PureComponent } from "react";
import { CSSTransition } from "react-transition-group";
import "./style.css";
export class App extends PureComponent {
constructor() {
super();
this.state = {
isShowTitle: true,
};
}
render() {
const { isShowTitle } = this.state;
return (
<div>
<button
onClick={(e) =>
this.setState({
isShowTitle: !isShowTitle,
})
}
>
change
</button>
<CSSTransition
in={isShowTitle}
classNames="csst"
timeout={2000}
unmountOnExit={true}
appear
onEnter={(e) => console.log("on enter")}
onEntering={(e) => console.log("on entering")}
onEntered={(e) => console.log("on entered")}
onExit={(e) => console.log("on exit")}
onExiting={(e) => console.log("on exiting")}
onExited={(e) => console.log("on exited")}
>
<h2>App</h2>
</CSSTransition>
</div>
);
}
}
export default App;
.csst-apear {
transform: translateX(-150px);
}
.csst-apear-active {
transform: translateX(0);
transition: transform 2s ease;
}
.csst-apear .csst-enter {
opacity: 0;
}
.csst-apear-active .csst-enter-active {
opacity: 1;
transition: opacity 2s ease;
}
.csst-exit {
opacity: 1;
}
.csst-exit-active {
opacity: 0;
transition: opacity 2s ease;
}
基本使用
import React, { PureComponent } from "react";
import { SwitchTransition, CSSTransition } from "react-transition-group";
import "./style.css";
export class App extends PureComponent {
constructor() {
super();
this.state = {
isLogin: true,
};
}
render() {
const { isLogin } = this.state;
return (
<div>
<SwitchTransition mode="out-in">
<CSSTransition
key={isLogin ? "exit" : "login"}
classNames="login"
timeout={1000}
>
<button
onClick={(e) =>
this.setState({
isLogin: !isLogin,
})
}
>
{isLogin ? "退出" : "登录"}
</button>
</CSSTransition>
</SwitchTransition>
</div>
);
}
}
export default App;
.login-enter {
transform: translateX(100px);
opacity: 0;
}
.login-enter-active {
transform: translateX(0);
opacity: 1;
transition: all 1s ease;
}
.login-exit {
transform: translateX(0);
opacity: 1;
}
.login-exit-active {
transform: translateX(-100px);
opacity: 0;
transition: all 1s ease;
}
import React, { PureComponent } from "react";
import { CSSTransition, TransitionGroup } from "react-transition-group";
import "./style.css";
export class App extends PureComponent {
constructor() {
super();
this.state = {
books: [
{ id: 1, name: "book1", price: 123, count: 99 },
{ id: 2, name: "book2", price: 223, count: 99 },
{ id: 3, name: "book3", price: 323, count: 99 },
{ id: 4, name: "book4", price: 423, count: 99 },
{ id: 5, name: "book5", price: 523, count: 99 },
],
};
}
addNewBook() {
const books = [...this.state.books];
books.push({
id: new Date().getTime(),
name: "book6",
price: 111,
count: 99,
});
this.setState({
books,
});
}
delBook(index) {
const books = [...this.state.books];
books.splice(index, 1);
this.setState({
books,
});
}
render() {
const { books } = this.state;
return (
<div>
<h2>书籍列表</h2>
<TransitionGroup component="ul">
{books.map((item, index) => {
return (
<CSSTransition key={item.id} classNames="book" timeout={1000}>
<li>
<span>
{item.name} - {item.price}
</span>
<button onClick={(e) => this.delBook(index)}>删除书籍</button>
</li>
</CSSTransition>
);
})}
</TransitionGroup>
<button onClick={(e) => this.addNewBook()}>添加新书籍</button>
</div>
);
}
}
export default App;