Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@itzsaga/use-lazy-pagination

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@itzsaga/use-lazy-pagination

Apollo useLazyQuery bi-directional pagination hook.

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

@itzsaga/use-lazy-pagination

Greenkeeper badge npm version

Bi-directional pagination hook for use in React using GraphQL & the Relay cursor-based pagination. Expectin an Apollo useLazyQuery function. Thank to a blog post by Tim Specht for the inspiration.

Installation

$ npm i @itzsaga/use-lazy-pagination

or

$ yarn add @itzsaga/use-lazy-pagination

Usage

The hook expects a single argument, the query function returned from the @apollo/react-hooks useLazyQuery hook.

const { goBack, goForward } = useLazyPagination(queryPosts);

Return

The hook returns an object with 4 things on it.

Return ValueDescription
currentPageThe current page you are on in the stack, defaults to 1
goBackA function that moves you back a page
goForwardA function that move you forward a page
resetPaginationA function that resets the pagination state

Note: goForward expects a single argument of the endCursor provided in the pageInfo from the query. This is how the stack is built.

Example

import React from "react";
import { useLazyQuery } from "@apollo/react-hooks";
import useLazyPagination from "@itzsaga/use-lazy-pagination";

function App() {
  const [ queryPosts, { data, error, loading } ] = useLazyQuery(QUERY_POSTS);
  const { currentPage, goBack, goForward, resetPagination } = useLazyPagination(
    queryPosts
  );

  if (error) return <div>Error.</div>;

  if (loading) return <div>Loading...</div>;

  return (
    <div>
      <button disabled={currentPage === 1} onClick={() => goBack()}>
        Back
      </button>
      <div>You are on page {currentPage}.</div>
      <button onClick={() => goForward(data.Posts.pageInfo.endCursor)}>
        Forward
      </button>
    </div>
  );
}

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/itzsaga/use-lazy-pagination. If you would like to help with this project see our Contributing doc for more info.

License

The app is available as open source under the terms of the MIT License.

Keywords

FAQs

Package last updated on 19 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc