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;
}
添加回答
舉報