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

@tamkeentech/react-infinite-scroll

v1.0.2

Published

Lightweight scroll controller library for React

Downloads

8

Readme

@TamkeenTech/react-infinite-scroll

Lightweight scroll controller library for React.js

NPM JavaScript Style Guide

Tamkeentech, Logo

Features

  • [x] Basic usage : for wrapping items and return callback function to update the items kindly note all update props and state managed by the user
  • [x] Invert functionality : which allows you to scroll from bottom to top and vice versa
  • [x] Async : use this in case you have an endpoint that relies on pagination, it will handle everything for you

Installation

npm install --save @tamkeentech/react-infinite-scroll

Getting Started

Basic usage : Demo

All you need to do is to import the component and provide the minimum props as following

import React from 'react';

import InfiniteScroll from '@tamkeentech/react-infinite-scroll';

class App extends React.Component {

  state = {
    isLoading: false,
    count: 0,
    data: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  }

  onLimitReached = () => {
    // your logic goes here
    // update isLoading, count and data
  }

  render() {
    const { isLoading, data, count } = this.state;

    return (
      <InfiniteScroll 
        // The minimum required props
        isLoading={isLoading}
        isDataFinished={count === 5}
        onLimitReached={this.onLimitReached}
        scrollThreshold={.1} // not required see props section
        render={() => data.map((item, i) => (
          <div key={i} style={{ height: 50, border: '1px solid red', marginBottom: 10 }}>
            {item}
          </div>
        ))}
      />
    );
  }
}

export default App;

Async usage : Demo

All you need to do is to provide the async object with the configuration that suits you but you have to know that if you didn't provide the request url the component will handle it as the previous [ Basic usage ]

Also in this case you will receive an object as a parameter for the render prop that holds the pagination status which consists of isLoading, data, error

import React from 'react';

import InfiniteScroll from '@tamkeentech/react-infinite-scroll';

class App extends React.Component {
  render() {
    return (
      <InfiniteScroll 
        // The minimum required props
        async={{
          // axios configs
          configs: {
            url: 'your endpoint',
          },
          // optional props
          dataTargetKey: '...',
          totalCountProp: '...',
          pageSizeProp: '...',
          pageSize: 80,
          page: 1,
        }}
        scrollThreshold={.1} // not required see props section
        render={(response) => response.data.map((item, i) => (
          <div key={i} style={{ height: 50, border: '1px solid red', marginBottom: 10 }}>
            {item.name}
          </div>
        ))}
      />
    );
  }
}

export default App;

Props

Shared Props

name | type | defaultValue | description -----------------|--------------------|------------------|-------------------- height | string, number | 100vh | height of the scrollable container className | any | '' | wrapper className scrollThreshold | number 0 to 1 | 0.2 | calculate the distance in which you will fetch the data see Notes section to know it's calculated inverse | boolean | false | To make scroll from bottom to top and viceversa enableLoader | boolean | true | To show the loader or hide it loader | React.Component | 'isLoading....' | Loader indicator debug | boolean | false | used to log the state, props, api call(success, error, distance at which the onScroll callback will work again, and more) render | function | () {} | function that returns the children

Basic usage Props name | type | defaultValue | Required | description -----------------|-------------|------------------|--------------|----------------- isLoading | boolean | false | true | To show the loader when the limit reached while there is feaching in progress, to avoid infinite loop of fetching data as well isDataFinished | boolean | false | true | To control wheather there is a data to fetch or not, if true -----> onLimitReached() will not be called onLimitReached | function | () {} | true | will be fired when isDataFinished = false and scrollOffsetBottom < calculated scrollLimit based on scrollThreshold

Async usage Props name | type | defaultValue | description ----------|-------------|------------------|-------------------- async | object | {} | It used for advanced usage when ur data and pagination relies on the server see the below props to know what you have to provide

async object props name | type | defaultValue | Required | description ----------------|-------------|------------------|--------------|----------------- configs | object | axios configs | true | axios request configs like (method, headers, bearer, ...etc), you have to provide url prop in it otherwise the component will fallback to Basic usage state dataTargetKey | string | 'items' | false | Key to target the data from response in order to iterate over totalCountProp | string | 'totalCount' | false | total items from server in order to calculate isDataFinished automatically pageProp | string | 'page' | false | page prop to send it as a query like, endpoint?page=3 pageSizeProp | string | 'pageSize' | false | page size prop to send it as a query like, endpoint?page=3&pageSize=20 page | number | 0 | false | initial request page, sometimes it starts from 0 or 1, it depends on your endpoint configuration pageSize | number | 20 | false | number of rows you need per request or per page

Notes

  • In case of Async the render(response) will receive an object as a parameter that holds isLoading, data, error
  • Distance of scroll from the end calculated based on scrollThreshold as the following
  const scrollThreshold = 0.2;
  const wrapperScrollHeight = 1000; // the height of the content in the wrapper including the hidden elements

  return scrollThreshold * wrapperScrollHeight; // 200px from the end of scrolling 

License

MIT ©

Author

MohammedSaber