reactjs - 在 React.js 中获取 API 获取的数据后加载组件
问题描述
我的组件代码如下
componentDidMount = () => {
this.props.dispatch(getCountry());
}
render() {
let rows = this.props.countries.map((item, index) => (//some code here));
return (
<div>
{rows ? (
//components HTML code here
) : (
<img src="loading.gif" alt="Loading ..."/>
)}
</div>
)
}
但是组件在 API 获取数据之前加载。
解决方案
rows
是一个数组,因此!rows
仍然是正确的。试试这个 :
return (
<div>
{rows && rows.length ? (
//components HTML code here
) : (
<img src="loading.gif" alt="Loading ..."/>
)}
</div>
)
推荐阅读
- mysql - 关于多对多关系的 Ruby On Rails 从 sqlite3 到 mysql 的数据迁移
- python - 安装时linux终端错误
- c# - ArgumentException:不支持关键字:'persistsecurity info' .NET Core MVC 2.0
- powerpoint - 如何在用户单击或按下任何按钮时删除上一个项目
- linux - 临时屏幕输出会话存储在哪里
- c# - 根据 id 获取绑定对象
- java - 从不同的类调用时,Java repaint() 不起作用
- android - 更改按钮颜色会导致按钮变大
- apache-spark - 如何使用 YARN 客户端模式在 Jupyter 中以交互方式运行 Pyspark?
- sorting - 将 jinja 列表中的值四舍五入