简体   繁体   English

React + Framer Motion 淡出功能组件

[英]React + Framer Motion fading out functional component

So I created this preloader that shows when the var loading = true but I want the loader to fade out.所以我创建了这个预加载器,它显示 var loading = true 但我希望加载器淡出。 This is what my files look like.这就是我的文件的样子。

Home page with all the stuff.包含所有内容的主页。

       return (
        <>
            {loading ? (
              <Loading />
            ) : (
              <>
                <Navbar />
                <Hero />
                <About />
                <Skills />
                <Contact />
              </>
            )}
        </>
      );

Loading function file加载 function 文件

const Loading = () => {
  const pathVariants = {
    hidden: {
      opacity: 0,
      pathLength: 0,
    },
    visible: {
      opacity: 1,
      pathLength: 1,
      transition: {
        duration: 2,
        ease: 'easeInOut',
      },
    },
  };
  const cVariants = {
    hidden: {
      opacity: 0,
    },
    visible: {
      opacity: 1,
      transition: {
        duration: 1,
        ease: 'easeInOut',
        delay: 2,
      },
    },
  };

  return (
    <>
      <AnimatePresence>
        <motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>
          <PreLoader>
            <Header>
              <motion.svg
                width='286'
                height='209'
                viewBox='0 0 286 209'
                fill='none'
                xmlns='http://www.w3.org/2000/svg'
              >
                <motion.g variants={cVariants} initial='hidden' animate='visible'>
                  <path
                    id='C'
                    d="svglol"
                    fill='#FF564A'
                  />
                </motion.g>

                <motion.g>
                  <motion.path
                    id='Polygon 1'
                    d="svglol"
                    stroke='#FF564A'
                    variants={pathVariants}
                    initial='hidden'
                    animate='visible'
                  />
                </motion.g>
              </motion.svg>
            </Header>
          </PreLoader>
        </motion.div>
      </AnimatePresence>
    </>
  );
};

export default Loading;导出默认加载;

So how can I make it so that the Loading function fades out?那么我怎样才能使 Loading function 淡出呢? I tried many things such as AnimatePresence wrapped around the Loader itself but that doesn't seem to work.我尝试了很多东西,例如围绕加载器本身包裹的 AnimatePresence,但这似乎不起作用。 (I am also using styled-components btw) (顺便说一句,我也在使用样式组件)

    return (
        <AnimatePresense>
            {loading ? (
              <Loading key="loading" />
            ) : (
              <React.Fragment key="main">
                <Navbar />
                <Hero />
                <About />
                <Skills />
                <Contact />
              </ React.Fragment>
            )}
        </AnimatePresense>
      );

Tips to get this working让这个工作的提示

  1. AnimatePresense must be outside of what you are conditionally rendering AnimatePresense 必须在您有条件渲染的范围之外
  2. You must have explicit keys on children of AnimatePresense because this is how React knows if the component has changed or not.您必须在 AnimatePresense 的子项上具有显式键,因为这是 React 知道组件是否已更改的方式。

I know it is boring, but I might reread the AnimatePresense doc a couple of times.我知道这很无聊,但我可能会重读几次 AnimatePresense 文档。 I had to read it 2 or 3 times personally before I figured out all the gotchas在弄清楚所有问题之前,我必须亲自阅读 2 或 3 遍

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM