【发布时间】:2020-06-13 06:39:20
【问题描述】:
我正在使用 Electron React 应用程序,我很好奇是否需要异步函数来正确显示预加载器 svg。 svg 以状态存储,当我通过显示而不是删除它来测试它时,它工作得非常好。但是,当我实际尝试在我的应用程序中使用它时,它似乎从未出现过。
Here's what I'd like it to look like(我通过注释掉处理文件后将状态更改回 null 的位置来实现此目的)
Here's what I get instead(当我将状态从 loader 更改回 null 时会发生什么......看不到 loader!)
我的问题是,这是我需要异步函数来实现的吗?我对正确使用 async await 不是很熟悉,这不是我第一次在 react 应用程序中遇到这个问题。我通常只是想弄清楚为什么加载器在我按预期实现时似乎从未出现过。老实说,我可能只是在这里犯了一个新手错误,所以我想要第二个意见!
在 App.js 中:
const [loading, setLoading] = useState(null);
const handleLoading = (value) => {
setLoading(value);
};
...
<Route exact path="/list">
<List loading={loading} />
</Route>
<Route exact path="/">
<Start loading={handleLoading} />
</Route>
在 Start.js 中
const handleFiles = (files) => {
// *set the loading state to the component
props.loading(
<div className="base-loader">
<div className="preloader-wrapper big active">
<div className="spinner-layer spinner-blue-only">
<div className="circle-clipper left">
<div className="circle"></div>
</div>
<div className="gap-patch">
<div className="circle"></div>
</div>
<div className="circle-clipper right">
<div className="circle"></div>
</div>
</div>
</div>
</div>
);
// counter to list with a Materialize toast when finished if > 0
sendFiles(files);
if (incompatibleFiles > 0) {
M.toast({
html: `${incompatibleFiles} file(s) could not be minified due to incompatible file type.`,
});
}
// reset for next drag and drop
incompatibleFiles = 0;
// *THIS is the line I comment out for it to start working (although it just goes infinitely)
props.loading(null);
};
最后在 List.js 中,我只是在组件返回语句中包含 {props.loading}。
【问题讨论】:
-
您是否尝试在
setTimeout注释掉的行中添加延迟只是为了查看加载程序是否出现?有时加载是即时的,加载器只是没有时间显示。 -
另外,建议对加载状态使用布尔值,而不是
null。 -
使用 setTimeout 就像一个魅力!非常感谢,我会确保在未来使用它!
-
你介意接受我的回答吗?
标签: reactjs asynchronous state preloader