【发布时间】:2020-07-09 06:17:41
【问题描述】:
我有一个简单的功能组件,我单击一个按钮并显示它,我试图让我的导入微调器在单击按钮时显示 2 秒钟,然后在两秒钟后显示我的导入组件,但是我在单击按钮后,我只能让微调器显示 2 秒,并且无法让它停止
import React, { useState } from "react";
import Hello from "./Hello";
import Spinner from '../Spinner/Spinner'
import "./styles.css";
export default function App() {
const [show, setShow] = useState(false);
const [loading, setLoading] = useState(false);
const helloHandeler = () => {
setTimeout(() => {
setLoading(!loading)
}, 2000)
setShow(!show);
};
if (loading) return <Spinner />
return (
<div className="App">
<h1>Adding a Spinner</h1>
<div className="bodyContainer">
{!show && <button onClick={helloHandeler}>Click me</button>}
{show && <Hello />}
</div>
</div>
);
}
工作示例可以在这里找到:https://codesandbox.io/s/gallant-engelbart-y3jus
【问题讨论】:
标签: javascript reactjs react-functional-component use-state