[英]Css transition not working on conditional rendering in React
我有一个简单的复选框,onClick,我想使用 React 显示和隐藏一个 div 以及一些转换延迟。 不使用 React 时我也能达到同样的效果。 但是,当我在单击复选框时使用条件渲染来渲染 div 时。 它没有显示效果。 这是我到目前为止的代码
import { useState } from "react";
import "./styles.css";
export default function App() {
const [checkBox, setCheckBox] = useState(false);
return (
<div className="App">
<input
type="checkbox"
checked={checkBox}
onChange={() => setCheckBox(!checkBox)}
></input>
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
{checkBox && (
<div
style={{
opacity: 1,
transition: "height 2s",
transitionTimingFunction: "ease-in"
}}
>
Hey the checkbox is checked
</div>
)}
</div>
);
}
Codesandbox 链接 - https://codesandbox.io/s/reverent-cartwright-gk11s?file=/src/App.js:0-659
任何帮助深表感谢
这个对我有用。 试试这样。
import { useState } from "react";
import "./styles.css";
export default function App() {
const [checkBox, setCheckBox] = useState(false);
return (
<div className="App">
<input
type="checkbox"
checked={checkBox}
onClick={() => setCheckBox(!checkBox)}
></input>
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<div className={checkBox ? "animate show" : "animate"}>
Hey the checkbox is checked
</div>
</div>
);
}
在 css 文件中......
.animate {
height: 0px;
overflow: hidden;
transition: height 0.5s;
}
.show {
height: 18px;
}
请在此确认。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.