gpt4 book ai didi

html - 按钮加载 react-hooks-nextjs

转载 作者:太空宇宙 更新时间:2023-11-04 05:41:06 25 4
gpt4 key购买 nike

我正在做一个加载微调按钮,当我按下按钮时,加载图标会出现但不会在标记的时间消失,并继续加载。这是代码:

组件一:

import React from 'react'

函数加载器(){

return(
<>
<div className="loaderIcon"></div>
<style jsx>
{`
.loaderIcon{
border: 10px solid #f3f3f3;
border-radius: 50%;
border-top: 10px solid #3498bd;
width: 60px;
height: 60px;
animation: spin 2s linear infinite;
}
@keyframes spin{
0% { transform: rotate(0deg);}
100% { transform rotate(360deg);}
}
`}
</style>
</>
)

导出默认加载器;

组件二:

import React, {useState} from 'react';

从'../components/loader'导入加载器

函数 ButtonLoading(){

const [loading, setLoading] = useState(false)

function loadData(){
setLoading ({loading: false})

setTimeout(()=>{
setLoading({loading: true});
}, 1000);

}
return(
<>
<div>
<button className="btnLoading" onClick={loadData} disabled={loading}>
{loading &&(<Loader/>)}
{loading && <span className="oneSpan">Loading page</span>}
{!loading && <span className="twoSpan">Load page</span>}
</button>
</div>

<style jsx>
{`
.btnLoading{
background-color: green;
}
.oneSpan{
color: red;
font-size: 20px;
}

.twoSpan{
color: black;
font-size: 20px;
}
`}
</style>
</>
)

导出默认的 ButtonLoading;

最佳答案

我认为你的加载是倒退的。我相信这个函数应该看起来像`

function loadData(){
setLoading ({loading: true})

setTimeout(()=>{
setLoading({loading: false});
}, 1000);
}`

SetTimeout 在定时器启动后运行函数

关于html - 按钮加载 react-hooks-nextjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59220297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com