From 8d6347d6ca14fdd305b31dcd010a5700666ad2e2 Mon Sep 17 00:00:00 2001 From: developomp Date: Sun, 1 Aug 2021 11:15:44 +0900 Subject: [PATCH] optimization (removed a for loop) --- source/src/pages/PostList.tsx | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/source/src/pages/PostList.tsx b/source/src/pages/PostList.tsx index 584c404..c05ad86 100644 --- a/source/src/pages/PostList.tsx +++ b/source/src/pages/PostList.tsx @@ -61,27 +61,23 @@ export default class PostList extends React.Component< async componentDidMount() { const PostCards: Array = [] - const recentPosts = {} - let postIndex = 0 + let postCount = 0 for (const date in posts.date) { - if (postIndex == this.state.howMany) continue + if (postCount >= this.state.howMany) break const length = posts.date[date].length for (let i = 0; i < length; i++) { - if (postIndex == this.state.howMany) continue - postIndex++ + if (postCount >= this.state.howMany) break + + postCount++ const url = posts.date[date][length - i - 1] - recentPosts[postIndex] = [url, posts.posts[url]] + PostCards.push( + + ) } } - for (const postIndex in recentPosts) { - const [url, post] = recentPosts[postIndex] - - PostCards.push() - } - this.setState({ PostCards: PostCards, })