1 回答

TA贡献1826条经验 获得超6个赞
您应该在渲染它的组件的外部范围中定义Modal,动画不会完成,并且您可以通过在下一个渲染时重新定义它来重置它。
还应该重置动画none而不是给出实际长度。
此外,可能有更多相关的 CSS 错误可以隐藏您的modal动画,例如z-index和position,如果您的问题集中在动画问题上,您应该消除它周围的所有噪音。
请参阅工作示例:
const Animation = styled.div`
transform: ${({ animate }) => (animate ? "none" : "translateX(500px)")};
transition: transform 1s;
`;
function Modal(props) {
return <Animation animate={props.show}>hello</Animation>;
}
function Component() {
const [show, toggle] = useReducer((p) => !p, false);
return (
<>
<Modal show={show} />
<button onClick={toggle}>show</button>
</>
);
}
null另外,当您不想设置动画时,您不应该返回,否则您将丢失关闭动画。
// remove this code
if (!props.show) {
return null;
}
添加回答
举报