reactjs - 在没有 API 调用或设置超时的情况下在登录页面中显示和隐藏加载程序?
问题描述
我在使用 redux 的反应应用程序中苦苦挣扎,搜索了很多但没有得到所需的解决方案,这就是我在这里发布问题的原因,所以我的问题是我必须在页面加载时显示加载器HTML加载加载器应该隐藏,下面是我的代码
class LoginScreen extends React.Component {
state = {
email: '',
password: '',
stayLoggedIn: false,
isLoading: true
};
componentDidMount() {
setTimeout(() => {
this.setState({ isLoading: false });
}, 2000);
}
渲染基于isLoading
但目前我已经使用 set-timeout 显示和隐藏加载器我想知道是否有任何解决方案可以使其在没有 set-timeout 的情况下工作,任何帮助都是可观的。
解决方案
setTimeout
只是一个异步操作,当你调用 setState 函数时,React 会比较旧状态和新状态,并根据新状态触发对你的组件的渲染。当你的组件有一个本地状态时,你将 redux 状态作为 props 注入到你的组件中,这样当你有本地状态时不会触发渲染,你需要使用
componentWillReceiveProps
方法并调用setState
方法来改变你的本地状态setState
根据新的道具使用状态。注意:
componentWillReceiveProps
方法工作直到React v17
import React from "react";
import ReactDOM from "react-dom";
const delay = async ms => await new Promise(resolve => setTimeout(resolve, ms));
class Loader extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: false
};
}
componentWillReceiveProps(nextProps) {
this.setState({ isLoading: nextProps.isLoading });
}
render() {
return (
<div style={{ border: "1px solid #999", padding: "10px" }}>
Loader component
<div>Loading: {String(this.state.isLoading)}</div>
</div>
);
}
}
class LoginScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: false
};
}
async asyncOperation() {
// Show loader
this.setState({ isLoading: true });
// Async operation, like fetch, etc...
await delay(1000);
// Hide loader
this.setState({ isLoading: false });
}
render() {
return (
<div>
<Loader isLoading={this.state.isLoading} />
<br />
<button
onClick={this.asyncOperation.bind(this)}
disabled={this.state.isLoading}
>
{this.state.isLoading ? "Waiting..." : "Call async operation"}
</button>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<LoginScreen />, rootElement);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
推荐阅读
- android - 如何在保留在主屏幕上的小部件中打开另一个视图?
- linux - linux exit 1 vs false 命令之间的区别
- html - 在自动正确调整大小之前,轮播图像最初显示太小
- raspberry-pi - 树莓派 - canbus can-utils 超时?
- c# - 尝试在 ASP.Net 中绑定表时 Ajax 给出随机结果
- python - 如何使用scrapy回调在两个蜘蛛之间传递参数
- sql - 在 Hive 的 LOCATION 字段中明确指定之前,不会创建数据库目录
- linux - 看不懂简单的 bash 脚本
- android - 使用 kotlin 语言向 android studio 中的 Activity 添加和管理两个/多个布局
- php - 查看 [邮件] 未找到 Laravel 5.4