React Transition Group -- TransitionGroup 组件

导语

Transition组件
CSSTransition组件css

该库的前两个基本组件能够查看以上连接🔗,今天搬到了新的房子里,仍是很满意的,明天就要去新公司报道了,但愿能和新同事好好相处,完成2019年的几个目标。react

TransitionGroup

<TransitionGroup>组件管理列表中的一组<Transition>组件。与<Transition>相似,<TransitionGroup>是一种状态机,用于管理组件随时间的安装和卸载。segmentfault

下面的例子使用了以前的渐变CSS过渡。当项目被删除或添加到TodoList时,in属性会被自动切换。能够在<TransitionGroup>中使用任何<Transition>组件,而不单单是css。动画

第一步: 导入须要的文件this

import React,{ Component } from 'react'
import { CSSTransition, TransitionGroup } from 'react-transition-group'
import './css/index.css'

第二步:编写初始state里的列表项spa

state = {
    items: [
      { id: 1, text: 'Buy eggs' },
      { id: 2, text: 'Pay bills' },
      { id: 3, text: 'Invite friends over' },
      { id: 4, text: 'Fix the TV' },
    ]
  }

第三步:编写列表项code

<TransitionGroup className="todo-list">
  {items.map(({ id, text }) => (
    <CSSTransition
      key={id}
      timeout={500}
      classNames="show">
      <div className="todo-list-item">
        <button
          className='cancle'
          onClick={() => {
            this.setState(state => ({
              items: state.items.filter(
                item => item.id !== id
              ),
            }));
          }}>
          &times;
        </button>
        <span className='item-text'>{text}</span>
      </div>
    </CSSTransition>
  ))}
</TransitionGroup>

记住,要把全部须要管理的列表项都写在<TransitionGroup> 中,其次,<CSSTranstion> 组件的in属性此时由<TransitionGroup> 管理了,不须要单独设置了,当点击删除按钮时,list中的对应项将会被删除,对应项的in 属性将会被自动改成false,从而触发离场动画。component

第四步:添加按钮图片

<button
  className='add'
  onClick={() => {
    const text = prompt('Enter some text');
    if (text) {
      this.setState(state => ({
        items: [
          ...state.items,
          { id: 1123, text },
        ],
      }));
    }
  }}>
  Add Item
</button>

当点击添加按钮,输入文字后,将会把此项添加到列表中,此时in属性为true,同时默认设置了首次动画,因此会触发一次进场动画。ci

效果图

图片描述

完整代码

//index.js
import React,{ Component } from 'react'
import { CSSTransition, TransitionGroup } from 'react-transition-group'
import './css/index.css'

export default class App extends Component {

  state = {
    items: [
      { id: 1, text: 'Buy eggs' },
      { id: 2, text: 'Pay bills' },
      { id: 3, text: 'Invite friends over' },
      { id: 4, text: 'Fix the TV' },
    ]
  }

  render () {
    const { items } = this.state
    return (
      <div className='container'>
        <TransitionGroup className="todo-list">
          {items.map(({ id, text }) => (
            <CSSTransition
              key={id}
              timeout={500}
              classNames="show"
              unmountOnExit>
              <div className="todo-list-item">
                <button
                  className='cancle'
                  onClick={() => {
                    this.setState(state => ({
                      items: state.items.filter(
                        item => item.id !== id
                      ),
                    }));
                  }}>
                  &times;
                </button>
                <span className='item-text'>{text}</span>
              </div>
            </CSSTransition>
          ))}
        </TransitionGroup>
        <button
          className='add'
          onClick={() => {
            const text = prompt('Enter some text');
            if (text) {
              this.setState(state => ({
                items: [
                  ...state.items,
                  { id: 1123, text },
                ],
              }));
            }
          }}>
          Add Item
        </button>
      </div>
    )
  }
}

//index.css
.show-enter {
    opacity: 0.01;
  }
.show-enter-active {
    opacity: 1;
    transition: all 300ms ease-out;
}
.show-exit {
    opacity: 1;
}
.show-exit-active {
    opacity: 0.01;
    transition: all 300ms ease-out;
}

.container {
    position: absolute;
    top: 20px;
    left: 100px;
    padding: 20px;
    border-radius: 5px;
    box-shadow: 0 0 10px 1px rgb(202, 202, 202);
}

.todo-list {
    border-radius: 5px;
    box-shadow: 0 0 5px 1px rgb(202, 202, 202);
}

.todo-list-item {
    height: 35px;
    line-height: 35px;
    padding: 0 10px;
    border-bottom: 1px solid rgb(202, 202, 202);
}

.todo-list-item:last-of-type {
    border-bottom: 0;
}

.item-text {
    margin-left: 10px;
}

.cancle {
    border: 0;
    color: #fff;
    background-color: #F04134;
    border-radius: 3px;
    box-shadow: 0 0 5px 1px rgb(202, 202, 202);
}

.add {
    border: 0;
    height: 30px;
    line-height: 30x;
    width: 120px;
    margin-top: 15px;
    font-size: 14px;
    border-radius: 3px;
    box-shadow: 0 0 5px 1px rgb(202, 202, 202);
}

Porps

<TransitionGroup> 的经常使用属性主要就2个,其它的有兴趣能够去官网看看 👀

component

default 'div' 也就是TransitionGroup渲染出来的标签为div,也能够就行更改,例如component="span" 渲染出来的就是span标签了

type: any
default: 'div'

children

至关于你给的组件,能够是字符串或者自定义组件等。

type: any

相关文章
相关标签/搜索