[英]Framer Motion exit and AnimatePresence not working in Nextjs?
[英]How to use Framer Motion <AnimatePresence> and React Portals?
我使用React Portals构建了一个 React Modal组件(参见上面的文档)。 在单击关闭按钮时卸载组件之前,我想使用AnimatePresence使用 Framer Motion 运行exit
动画。 不幸的是,我无法让它工作,需要帮助。
我将
exit={{ opacity: 0 }}
到<RenderModal/>
的孩子。 使用initial
和animate
的进入动画按预期工作。
<AnimatePresence>
包裹在 modal-root 元素周围<div id="root"></div>
<AnimatePresence>
<div id="modal-root"></div>
</AnimatePresence>
错误:目标容器需要 DOM 元素
<div id="modal-root">
<AnimatePresence></AnimatePresence>
</div>
const Modal = ({
title,
footer,
children,
}) => {
<AnimatePresence>
{isVisible
&& (
<RenderModal
title={title}
footer={footer}
hide={hide}
>
{children}
</RenderModal>
)}
</AnimatePresence>
};
return (
<>
<Button onClick={show}>Open Modal</Button>
<AnimatePresence>
<Modal {...args}></Modal>
</AnimatePresence>
<p>Lorem ipsum dolor sit amet...</p>
</>
);
你好,我想可能会迟到。 但我用这段代码解决了
import { ReactNode } from "react";
import ReactDOM from "react-dom";
import { AnimatePresence } from "framer-motion";
interface ModalWrapperProps {
children: ReactNode;
isShowing: boolean;
}
const ModalWrapper = ({ children, isShowing }: ModalWrapperProps) =>
ReactDOM.createPortal(
<AnimatePresence exitBeforeEnter>
{isShowing && children}
</AnimatePresence>,
document.body
);
export default ModalWrapper;
我启发了下面的帖子
https://blog.logrocket.com/implementing-animated-toasts-in-react/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.