繁体   English   中英

使用 Framer Motion React 在退出时为组件设置动画

[英]Animate Components On Exit With Framer Motion React

我有一个在两个组件之间切换的按钮。 如何在退出时为每个组件添加 animation? 这是我的代码不起作用:

export default function App() {
  const [dark, setDark] = useState(false);
  const toggle = () => {
    setDark(!dark);
  };

  return (
    <div>
      <AnimatePresence>
        {dark ? (
          <motion.h2 exit={{ opacity: 0 }}>Dark</motion.h2>
        ) : (
          <motion.h2 exit={{ opacity: 0 }}>Light</motion.h2>
        )}
      </AnimatePresence>
      <button onClick={toggle}>Toggle</button>
    </div>
  );
}

谢谢你的帮助!

现在已修复。 为了使 animation 工作,您应该添加初始动画道具。 此外,每个组件都需要有一个唯一的key

暂无
暂无

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

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