npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

load-more-reactjs-component

v1.0.0

Published

Load More or Lazy load Component

Downloads

44

Readme

Load More Component In ReactJS - A Recursive Approach

The problem…

During a project, we thought of creating a page which scrolls infinitely, i.e whenever the bottom of the page is reached, we load the next page and so on.

Initially we explored some similar open-source Components, but there was a common thing in all the available Components - they concat the data of each page and render them all cumulatively. Say for example, if the first page contains 20 items and a request is made for the next page. The data now contains 40 items which are rendered. With the 5th page’s request, 100 items are rendered (80 previous items and 20 new items). The re-rendering of the previous 80 items are redundant and unnecessary.

So that’s where we decided to create our own Component which handles this problem and is also multi-purpose through different combinations of props.

The solution…

After scratching our heads for a week and trying out different things, finally we came across React’s Recursive Components. There was an instinct that it could solve our problem. As the name suggests, a component rendered within the same component is called Recursive Component. With recursion, we achieved minimal re-rendering while loading the next pages.

Visualization of the solution…

recursive-lazy-visualization

Installation

npm i recursive-load-more-reactjs --save

Component’s Integration

<RecursivePagination
	paginationEntityIds = {a plain array}
	getNextPageData = {data provider function}
	uniqueId = {unique identifier}
	nextButtonLabel = {button text}
	preCallback = {function to call before loading}
	postCallback = {function to call after loading}
	isAutoLoad = {next page to be loaded lazily}
	bottomOffset = {offset of autoloading}
	LazyComponent={<YourComponent {...props} />}
/>

Props Details

| Prop | Description | Type | Default value | Is it required? | | ---- | --------------------------- | ---- | ------------- | --------------- | | LazyComponent | It is the page’s Component which we need to repeat. | React Component | - | Yes | | paginationEntityIds | This prop is an array of page IDs/page numbers. | Plain array | Empty array | Yes | | getNextPageData | API call for the next page is made in this function. It returns a Promise. | Function | - | Yes | | nextButtonLabel | It’s the label of the next button. | String | ‘Load More’ | No | | uniqueId | Unique identifier for the Component. | String | - | No | | preCallback | A callback which is called just before making an API call for the next page. | Function | - | No | | postCallback | A callback which is called just after the API call is resolved. | Function | - | No | | isAutoLoad | A flag which tells the component to load the next page automatically when bottom is reached keeping a margin. | Boolean | False | No | | bottomOffset | It’s the margin to keep when the isAutoLoad prop is set to true. | String | ‘100px’ | No |