reactjs - React Redux:更新时组件内部内容之间的延迟
问题描述
我有一个组件,里面有 2 个组件:
MyComp {
render (
html of My Comp..
<Loading show={this.props.isLoading}/>
<ErrorMessage show={this.props.hasError}/>
)
}
接收数据时,显示Loading。加载完成后,它会收到如下信息:
{
isLoading: false,
hasError: true
}
但在屏幕上,加载在 hasError 显示前 2 秒关闭。
这两个组件都采用相同的策略构建:
class Loading extends Component {
constructor(props) {
super(props);
this.state = {isLoading : props.show};
}
componentWillReceiveProps(nextProps) {
this.setState({ isLoading: nextProps.show });
}
render() {
if (this.state.isLoading) {
return (
<div className="loading">
<div className="loading-message">
Carregando...
</div>
</div>);
}
return ('');
}
}
export default Loading;
解决方案
不完全是这个问题的答案,因为我不确定延迟来自哪里。
但是根据您的代码,我建议不要使用本地state
并尝试将其与外部同步props
。
这可能会导致错误(可能与您的问题有关?),因为componentWillReceiveProps
即使没有props
收到新消息也会被调用,除了它自反应 V16.2 以来 处于弃用过程中。
相反,我会直接从this.props
:
class Loading extends Component {
render() {
if (this.props.isLoading) {
return (
<div className="loading">
<div className="loading-message">
Carregando...
</div>
</div>);
}
return ('');
}
}
同样,不确定它是否与您的问题直接相关,但这是一种更好的做法。
推荐阅读
- java - 将 CSV 文件转换为 XLS 文件,逗号有问题
- php - 将“显示单一变体”代码添加到 functions.php 时出错
- sql-server - 无法在 T-SQL CTE 中增加值
- firebase - 退出 Flutter 应用程序不会返回登录页面
- github - 部署失败 - 添加子模块问题 - 使用 Netlify
- bash - 如何在特定位置对具有匹配字符的列求和?
- webforms - 如何使用 ASP.Net WebForms 自动化构建和部署?
- python - 如何使用 OpenCV 检测物体的边缘
- javascript - 为什么圆圈在画布上沿 x 和 y 方向移动?
- r - 在 R 中,如何让循环函数在每行都存在 site_id 的唯一站点上工作?