I am using an API to retrieve latest posts from Firestore. The API is something is like this
function loadFeedPosts(
createdAtMax,
limit
) {
return db
.collection("posts")
.orderBy("createdAt", "desc")
.where("createdAt", "<", createdAtMax)
.limit(limit)
.get()
.then(getDocsFromSnapshot)
}
createdAtMax
is the time we need to specify in order to retrieve the posts.
And I have a Feed
component. It looks like this.
function Feed() {
const [posts, setPosts] = useState([])
const [currentTime, setCurrentTime] = useState(Date.now())
const [limit, setLimit] = useState(3)
useEffect(() => {
loadFeedPosts(currentTime, limit).then(posts => {
setPosts(posts)
})
}, [currentTime, limit])
return (
<div className="Feed">
<div className="Feed_button_wrapper">
<button className="Feed_new_posts_button icon_button">
View 3 New Posts
</button>
</div>
{posts.map(post => (
<FeedPost post={post} />
))}
<div className="Feed_button_wrapper">
<button
className="Feed_new_posts_button icon_button"
onClick={() => {
setLimit(limit + 3)
}}
>
View More
</button>
</div>
</div>
)
}
So when people click on the View More
button, it will go retrieve 3 more posts.
My question is, since clicking on the View More
button triggers a re-render, I assume the line const [currentTime, setCurrentTime] = useState(Date.now())
will get run again, does the Date.now()
which serves as the initial value for currentTime
get evaluated again for every re-render?
I tested it myself by logging out currentTime
, and it seemed not updating for every render. According to the React doc https://reactjs.org/docs/hooks-reference.html#lazy-initial-state, I thought only when using Lazy initial state, the initialState
is only calculated once. But here I am not using Lazy initial state, how come it is not calculated every time the component re-renders?
Although the intended behavior of the initial state of currentTime
is exactly stays the same for every re-render, I am just baffled why it didn't get re-calculated for every re-render since I am not using Lazy initial state