hos*_*seh 2 transition hide reactjs
我试图在 ReactJS 上几秒钟后隐藏一个 div 。到目前为止,这是我的代码:
setTimeout(function() {
$('#submitmsg').fadeOut('fast');
}, 3000);
<div id="submitmsg">
{message && <span> Messaged received. I'll respond to your query ASAP! </span> }
</div>
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,说“$”未定义。
$是反应中 jquery 的标志,
我们会说
document.getElementById('#submitmsg').fadeOut('fast');
Run Code Online (Sandbox Code Playgroud)
我也将使用一种更简单的方法,声明布尔值是 usestate
const Component = () =>{
const [showElement,setShowElement] = React.useState(true)
useEffect(()=>{
setTimeout(function() {
setShowElement(false)
}, 3000);
},
[])
return(
<div>
{showElement?<div>I'm here and i will be gone</div>:<></>}
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
这是整个代码,当我尝试它时,它工作正常,正如您在上面的codesandbox中看到的那样
import React, { useEffect } from "react";
const MyComponent = () => {
const [showElement, setShowElement] = React.useState(true);
useEffect(() => {
setTimeout(function () {
setShowElement(false);
}, 10000);
}, []);
return (
<div>
<div>
{showElement ? (
<div
style={{
height: "100vh",
background: "black",
color: "white",
fontSize: "30px",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
fontFamily: "roboto",
opacity: showElement ? 1 : 0
}}
>
I'm here and i will be gone
</div>
) : (
<div></div>
)}{" "}
</div>
</div>
);
};
export default MyComponent;
Run Code Online (Sandbox Code Playgroud)
https://codesandbox.io/s/thirsty-rosalind-o9pds?file=/src/App.js
| 归档时间: |
|
| 查看次数: |
12468 次 |
| 最近记录: |