react-总结5-高阶组件封装动画

高阶组件/Higher-Order Components

Higher-Order Components就是一个函数,传给它一个组件,它返回一个新的组件。
​ 1. 进行某些方法或是属性的复用

​ 2. 让外层的组件替我们完成任务,那么里层组件直接使用就可以了

举个简单的例子
import React from 'react'
import A from './components/A'

export const HocComp = comp => {
	return class newcomp extends React.Component {
		xx = () => {//表示某个公用的方法
			
		}
		render(){
			return (	//生成一个新的组件  这个组件包含公用的方法
				<A/ xx = { this.xx }>
			)
		}
	}
}


使用是就调用这个方法  传入组件做参数 生成新的组件并赋值就好了

实现react动画

在这里插入图片描述

1.安装 react-transition-group   yarn add react-transition-group
2.安装 animate.css     yarn add animate.css
3.在组件上方引入 import { CSSTransition } from 'react-transition-group' 将需要动画的标签放入 CSSTransition这标签内
组件内部
import React,{ Component, Fragment } from 'react'
import { AnimateHoc } from '../../hoc'
import Content from './content'


const HocCont = AnimateHoc(Content)

class AnimateComp extends Component{

    constructor( props ){
        super( props )
        this.state = {
            flag:true
        }
    }

    changeFlag = () => {
        
        const {flag} = this.state
        
        this.setState({
            flag:!flag
        })
    }

    render(){
        const { flag } =this.state
       
        return (
            <Fragment>
                <button onClick = { this.changeFlag }>切换</button>
                <HocCont flag = { flag }></HocCont>
            </Fragment>
        )
    }

}

export default AnimateComp
---------------------------------------------------------------------------
hoc封装
import React from 'react'
import {CSSTransition} from 'react-transition-group'
import 'animate.css'

export const AnimateHoc = Comp => {
    return class withAnimate extends React.Component {

        render(){
            
            const { flag } = this.props

            return (
                <CSSTransition
                    in = { flag }
                    timeout = { 2000 }
                    unmountOnExit
                    classNames = { {
                        enter: 'animated',
                        enterActive: 'slideInLeft',
                        exit: 'animated',
                        exitActive: 'bounceOut',
                    } }
                >
                    <Comp/>
                </CSSTransition>
            )

        }

    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值