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

use-relay-pagination

v1.0.2

Published

[![npm version][npm-version-src]][npm-version-href] [![Dependencies][david-dm-src]][david-dm-href] [![npm downloads][npm-downloads-src]][npm-downloads-href] [![code style: prettier](https://img.shields.io/badge/code_style-prettier-1a2b34.svg?style=flat-sq

Downloads

5

Readme

use-relay-pagination

npm version Dependencies npm downloads code style: prettier License: MIT

This is a React hook library to assist with GraphQL Relay pagination. The library provides a useRelayPagination hook that can be used to retrieve pagination parameters such as first, last, after, and before.

Installation

This library is designed to be used in a Next.js application. To install the library, simply run the following command:

yarn add use-relay-pagination

Usage

To use the useRelayPagination hook, import it into your component and call it like any other React hook:

import { useRelayPagination } from 'use-relay-pagination';

function MyComponent() {
  const { first, last, after, before } = useRelayPagination(10);

  // ...
}

The useRelayPagination hook accepts a single argument, which is the number of items to display per page (default is 20).

The hook returns an object containing the following properties:

  • first: The number of items to fetch from the beginning of the list.
  • last: The number of items to fetch from the end of the list.
  • after: The Relay cursor for the item after which to start fetching.
  • before: The Relay cursor for the item before which to start fetching.

These properties can then be used to construct your GraphQL query for pagination.

Examples

query MyQuery($first: Int, $last: Int, $after: String, $before: String) {
  items(first: $first, last: $last, after: $after, before: $before) {
    edges {
      node {
        id
        name
      }
      cursor
    }
    pageInfo {
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
  }
}

Here's an example of how you might use the useRelayPagination hook in your component:

import { useRelayPagination } from 'use-relay-pagination';
import { useQuery } from 'react-query';
import { MyQuery } from './MyQuery';

function MyComponent() {
  const { first, last, after, before } = useRelayPagination(10);

  const { data, isLoading, isError } = useQuery(['myQuery', { first, last, after, before }], () =>
    fetch('/graphql', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        query: MyQuery,
        variables: { first, last, after, before },
      }),
    }).then((res) => res.json())
  );

  // ...
}

In this example, we use the useQuery hook from react-query to fetch data from our GraphQL server. We pass in an array as the first argument to useQuery, which contains the query key and variables. The fetch call inside the useQuery hook is then used to fetch the data from the server. We pass in the MyQuery and variables to the body of the request.

The isLoading and isError properties of the useQuery hook can be used to display loading and error states in your component.

Development

  1. Clone this repository
  2. Install dependencies using yarn install
  3. Build the module using yarn build
  4. Start development server using yarn dev

📑 License

This package is licensed under the MIT License

Notes

README proudly generated by ChatGPT :robot:

To do

  • [ ] Add tests & CI
  • [ ] Support other router frameworks (outside Next.js)
  • [ ] Release bot