1. What is it?
In daily development, the transition animation when switching pages is a relatively basic scene
When a component has a transition animation in the process of displaying and disappearing, it can increase the user experience very well
react
There are many options for implementing transition animation effects in , such as , react-transition-group
, react-motion
, Animated
and native ones CSS
can complete switching animations
2. How to achieve
In react
, react-transition-group
is a good solution, which adds enter
, enter-active
, exit
, exit-active
this series of hooks to the element
It can help us realize the entry and exit animation of components conveniently
It mainly provides three main components:
- CSSTransition: In front-end development, combine CSS to complete the transition animation effect
- SwitchTransition: Use this component when the two components display and hide the switch
- TransitionGroup: Wrap multiple animation components in it, generally used for animation of elements in the list
CSSTransition
The principle of implementing animation is that when CSSTransition
the attributein
is set to , firstly, it will add and perform animation to its subcomponentstrue
CSSTransition
xxx-enter
xxx-enter-active
class
When the animation finishes executing, both are removed class
, and the -enter-done
addedclass
So you can take advantage of this, through css
the transition
attribute, let the element transition smoothly between the two states, so as to get the corresponding animation effect
When in
the property false
is , CSSTransition
the child component will be added with xxx-exit
the xxx-exit-active
sum class
, and then the animation will start. When the animation is over, remove the two class
, and then add -enter-done
theclass
For example:
export default class App2 extends React.PureComponent {
state = {show: true};
onToggle = () => this.setState({show: !this.state.show});
render() {
const {show} = this.state;
return (
<div className={'container'}>
<div className={'square-wrapper'}>
<CSSTransition
in={show}
timeout={500}
classNames={'fade'}
unmountOnExit={true}
>
<div className={'square'} />
</CSSTransition>
</div>
<Button onClick={this.onToggle}>toggle</Button>
</div>
);
}
}
The corresponding css
styles are as follows:
.fade-enter {
opacity: 0;
transform: translateX(100%);
}
.fade-enter-active {
opacity: 1;
transform: translateX(0);
transition: all 500ms;
}
.fade-exit {
opacity: 1;
transform: translateX(0);
}
.fade-exit-active {
opacity: 0;
transform: translateX(-100%);
transition: all 500ms;
}
SwitchTransition
SwitchTransition
A cool animation that can complete the switch between two components
For example, there is a button that needs to switch between on
and off
, we want to see on
exit from the left first, off
and then enter from the right
SwitchTransition
There is mainly one attribute in mode
, which corresponds to two values:
- in-out: Indicates that new components enter first, and old components are removed;
- out-in: Indicates that the component is removed first, and the new component is re-entered
SwitchTransition
There must be in the component CSSTransition
, you cannot directly wrap the component you want to switch
The components inside CSSTransition
no longer accept attributes to determine the state of the element as before in
, and are replaced by key
attributes
An example of button entry and exit is given below, as follows:
import { SwitchTransition, CSSTransition } from "react-transition-group";
export default class SwitchAnimation extends PureComponent {
constructor(props) {
super(props);
this.state = {
isOn: true
}
}
render() {
const {isOn} = this.state;
return (
<SwitchTransition mode="out-in">
<CSSTransition classNames="btn"
timeout={500}
key={isOn ? "on" : "off"}>
{
<button onClick={this.btnClick.bind(this)}>
{isOn ? "on": "off"}
</button>
}
</CSSTransition>
</SwitchTransition>
)
}
btnClick() {
this.setState({
isOn: !this.state.isOn})
}
}
css
The files correspond to the following:
.btn-enter {
transform: translate(100%, 0);
opacity: 0;
}
.btn-enter-active {
transform: translate(0, 0);
opacity: 1;
transition: all 500ms;
}
.btn-exit {
transform: translate(0, 0);
opacity: 1;
}
.btn-exit-active {
transform: translate(-100%, 0);
opacity: 0;
transition: all 500ms;
}
TransitionGroup
When there is a group of animations, these can be CSSTransition
put into one TransitionGroup
to complete the animation
There is also CSSTransition
no attribute in it , and the attribute in
is usedkey
TransitionGroup
When the perception children
changes, save the removed node first, and remove it after the animation ends
It is handled as follows:
-
Inserted nodes, first render dom, and then do animation
-
Deleted nodes, do animation first, then delete dom
as follows:
import React, { PureComponent } from 'react'
import { CSSTransition, TransitionGroup } from 'react-transition-group';
export default class GroupAnimation extends PureComponent {
constructor(props) {
super(props);
this.state = {
friends: []
}
}
render() {
return (
<div>
<TransitionGroup>
{
this.state.friends.map((item, index) => {
return (
<CSSTransition classNames="friend" timeout={300} key={index}>
<div>{item}</div>
</CSSTransition>
)
})
}
</TransitionGroup>
<button onClick={e => this.addFriend()}>+friend</button>
</div>
)
}
addFriend() {
this.setState({
friends: [...this.state.friends, "coderwhy"]
})
}
}
The correspondence css
is as follows:
.friend-enter {
transform: translate(100%, 0);
opacity: 0;
}
.friend-enter-active {
transform: translate(0, 0);
opacity: 1;
transition: all 500ms;
}
.friend-exit {
transform: translate(0, 0);
opacity: 1;
}
.friend-exit-active {
transform: translate(-100%, 0);
opacity: 0;
transition: all 500ms;
}