javascript - 不确定在哪里插入我的进度条组件以进行 api 调用
问题描述
- 我正在尝试学习材料ui
- 我得到了进度条。
- 但我需要加载直到我的数据没有加载。
- 不知道在哪里插入我的进度条组件。
- 你能告诉我如何在我调用 api 时修复它吗?以便将来我自己修复它
- 在下面提供我的代码片段。
- 我所有的代码都在 ReceipeReviewCardList.js
https://codesandbox.io/s/04r2qwv5jv
getPlayerValues() {
let comments = [];
fetch("https://jsonplaceholder.typicode.com/comments")
.then(response => response.json())
.then(json => {
comments = json;
// comments = comments.slice(0,3);
this.setState({ comments: comments });
this.setState({ activeComments: comments.slice(0, 10) });
//console.log(comments);
});
}
render() {
let listView = this.state.activeComments.map(comment => (
<RecipeReviewCard
key={comment.id}
commentId={comment.id}
cardBelowContent={comment.body}
cardContent={comment.name}
comment={comment}
/>
));
return this.state.comments.length > 0 ? (
<div>
<LinearDeterminate />
{listView}
<br />
<Pagination
activePage={this.state.activePage}
itemsCountPerPage={10}
totalItemsCount={this.state.comments.length}
pageRangeDisplayed={5}
onChange={this.handlePageChange}
/>
</div>
) : (
""
);
}
解决方案
最好的办法是使用一个loading
标志,该标志true
在您开始请求之前设置为,然后false
在请求返回时设置为。然后您可以使用该标志有条件地呈现您的进度条!
getPlayerValues() {
this.setState({ loading: true });
fetch("https://jsonplaceholder.typicode.com/comments")
.then(response => response.json())
.then(comments => {
this.setState({
loading: false,
comments: comments
});
});
}
render() {
const { comments, loading } = this.state
// active comments can be derived so no need to store them in state :)
const activeComments = comments.slice(0, 10);
// render your progress bar
if(loading) return <LinearDeterminate />;
// render everything else once loading === false
return (
<div>
{activeComments.map(comment => (
<RecipeReviewCard
key={comment.id}
comment={comment} {/* already passing comment prop here */}
commentId={comment.id} {/* so there's need to pass the these props */}
cardContent={comment.name} {/* just access via this.props.comment */}
cardBelowContent={comment.body}
/>
))}
</div>
)
}
推荐阅读
- c - 基础 C 快速排序
- flutter - 有什么方法可以禁用 YouTube iframe 上的长按?
- python-3.x - python3 - 睡眠时枚举结果中缺少线程
- apache - 上层 .htacces 文件中的代码被其他 .htaccess 文件中的代码忽略
- python - python winrt windows Toast通知获取输入作为变量
- c++ - 如何与在 SFML-IMGUI 中创建的按钮相关联?
- javascript - 使用来自 api 的数据创建填充复杂对象的最佳方法是什么?
- laravel - 通过 DOM 将数据附加到表单?
- excel - 显示为分数且未计算的两列的总和
- css - 制作第一个 div 元素 posititon:absolute(relative to body) ,使我的后续 div 隐藏在我的第一个 div 下