1.下载

  1. yarn add react-transition-group

2.使用

  1. import React, { PureComponent } from 'react'
  2. import { CSSTransition,TransitionGroup } from 'react-transition-group'
  3. import './index.css'
  4. export default class index extends PureComponent {
  5. state={
  6. names:['codewhy','code','lilei']
  7. }
  8. render() {
  9. const {names}=this.state
  10. return (
  11. <TransitionGroup>
  12. {
  13. names.map((item,index)=>{
  14. return (
  15. <CSSTransition key={index} timeout={1000} classNames='item'>
  16. <div >{item}</div>
  17. </CSSTransition>
  18. )
  19. })
  20. }
  21. <button onClick={e=>this.addName()}> +name</button>
  22. </TransitionGroup>
  23. )
  24. }
  25. addName(){
  26. this.setState((state,props)=>{
  27. return{
  28. names:[...state.names,'wansgu']
  29. }
  30. })
  31. }
  32. }
  1. //index.css
  2. .item-enter{
  3. opacity: 0;
  4. transform: scale(.6);
  5. }
  6. .item-enter-active{
  7. opacity: 1;
  8. transform: scale(1);
  9. transition: all 500ms;
  10. }
  11. .item-enter-done{
  12. color:red;
  13. }
  14. .item-exit {
  15. opacity: 1;
  16. transform: scale(1);
  17. }
  18. .item-exit-active {
  19. opacity: 0;
  20. transform: scale(.6);
  21. transition: all 500ms;
  22. }
  23. .item-exit-done {
  24. opacity: 0;
  25. }