React-transition-group 之单组件过渡动画
2019-01-01 15:30
429 查看
<
4000
/path>
概述:
在react开发的过程中,有时候需要给特定的页面或组件单独加上过渡动画,特别是给某个路由页面增加动画的时候,需要在页面组件中添加比较多的操作,并且每次需要添加动画的时候都要写一遍,实在比较麻烦,同时也增加了代码量和可读性,出于这个理由,把单组件过渡进行封装显的非常必要,在这里,采用了HOC的形式
需求:
- 单个组件进入和离开时需要动画
- HOC应该给包裹过的组件添加一个prop 用于退出组件
废话不多说,直接上代码
先看使用方法
import React, { Component } from 'react'; import { withSingleTransition } from "../../../components/withTransition/with-single-transition/with-single-transition"; @withSingleTransition('page', 300) class page extends Component { goBack = () => { this.props.goBack().then(() => do something) // 注意 如果过渡的是路由,需要在then里显式的调用push 或 goBack方法,否则路由不会卸载 // this.props.goBack().then(() => this.props.history.goBack()) }; ..... ..... }
import React from 'react'; import {CSSTransition} from 'react-transition-group'; export function withSingleTransition(classNames = 'page', timeout = 500) { return function(WrappedComponent) { return class Comp extends React.Component { constructor(props) { super(props); this.state = { in: true }; this.goBack = this.goBack.bind(this); } goBack() { const that = this; return new Promise((resolve) => { that.setState({in: false}); setTimeout(() => { resolve() }, timeout) }); } render() { return ( <CSSTransition timeout={timeout} in={this.state.in} classNames={classNames} appear={true} unmountOnExit={true}> <WrappedComponent {...this.props} goBack={this.goBack}/> </CSSTransition> ) } } } }
在这里,因为实在组件内部使用过渡,所以过渡组件挂载的时候需要立即调用,设置appear={true}
所以最后,需要添加的样式为
xxx-appear、xxx-appear-active、 xxx-enter-done、xxx-exit、xxx-exit-active、xxx-exit-done
相关文章推荐
- React动画组件——React-Transition-group动画实现
- 基于ReactCSSTransitionGroup实现react-router过渡动画
- react过渡动画效果的实现,react-transition-group
- Vue 过渡(动画)transition组件案例详解
- 【React自制全家桶】八、React动画以及react-transition-group动画库的使用
- Vue过渡(动画)- transition组件
- React动画之react-transition-group使用
- 【React自制全家桶】八、React动画以及react-transition-group动画库的使用
- react的动画ReactCSSTransitionGroup
- React动画之react-transition-group使用
- 用react的ReactCSSTransitionGroup插件实现简单的弹幕动画
- 【CSS3】transition过渡和animation动画
- css3_实现动画设置其过渡过程中的效果_transition
- vue中组件的过渡动画及实现代码
- 不要忘了css3动画效果transform 和过渡效果 transition:属性 时间 运动效果 延迟时间;
- VUE—— transition(过渡&动画)的简单使用
- android过渡动画之makeSceneTransitionAnimation
- ReactCSSTransitionGroup
- android 动画 过渡动画 transition
- Vue动画--组件的过渡