React集成animate.css入场动画

最近研究起了前端技术,简直五花八门。随手记录:animate.css动画。
这个是自己研究着写的,好像只能支持入场动画,如何支持出场动画还得深入研究,我想应该差不多的思路,应该要用到getDerivedStateFromProps这个生命函数。

代码

import React from "react";

function Animation(props) {
    const { animation } = props;
    return (
        <div className={`animate__animated animate__${animation}`}>
            {props.children}
        </div>
    );
}

function withAnimation(WrappedComponent, animation) {
    return React.forwardRef((props, ref) => {
        return (
            <Animation animation={animation ? animation : "fadeIn"}>
                <WrappedComponent ref={ref} {...props} />
            </Animation>
        );
    });
}
export { Animation, withAnimation };

使用

使用Animation包裹需要动画的组件,animation属性传递动画名称;或者使用withAnimation高阶组件,第二个参数为animation动画名称。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容