optimization (removed a for loop)
This commit is contained in:
parent
05b1b63347
commit
8d6347d6ca
1 changed files with 8 additions and 12 deletions
|
@ -61,27 +61,23 @@ export default class PostList extends React.Component<
|
||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
const PostCards: Array<unknown> = []
|
const PostCards: Array<unknown> = []
|
||||||
const recentPosts = {}
|
|
||||||
|
|
||||||
let postIndex = 0
|
let postCount = 0
|
||||||
for (const date in posts.date) {
|
for (const date in posts.date) {
|
||||||
if (postIndex == this.state.howMany) continue
|
if (postCount >= this.state.howMany) break
|
||||||
|
|
||||||
const length = posts.date[date].length
|
const length = posts.date[date].length
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
if (postIndex == this.state.howMany) continue
|
if (postCount >= this.state.howMany) break
|
||||||
postIndex++
|
|
||||||
|
postCount++
|
||||||
const url = posts.date[date][length - i - 1]
|
const url = posts.date[date][length - i - 1]
|
||||||
recentPosts[postIndex] = [url, posts.posts[url]]
|
PostCards.push(
|
||||||
|
<PostCard postData={{ url: url, ...posts.posts[url] }} />
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const postIndex in recentPosts) {
|
|
||||||
const [url, post] = recentPosts[postIndex]
|
|
||||||
|
|
||||||
PostCards.push(<PostCard postData={{ url: url, ...post }} />)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
PostCards: PostCards,
|
PostCards: PostCards,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue