Skip to content
Advertisement

How can I create a hook that reacts to an event to load more data?

I am trying to create a functionality where if a user clicks on a LOAD MORE button, it returns more data.

I already have some code done but every time I click on the LOAD MORE button, it removes the first 12 items and sets the new 12 items, but I don’t want that, I want to keep the 12 old tems, It is just a regular load more functionality.

JavaScript

Besides that, I need to turn this into a hook which I am going to using across the whole application.

What am I missing?

Any ideas?

Advertisement

Answer

You do not need an array arrayForHoldingPosts instead just use setPostsToShow( [...postsToShow, ...slicedPosts]);

arrayForHoldingPosts is becoming empty array after every renders so old data is lost.

Hook example

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement