A simple React custom hook for pull-to-refresh function that support NextJs
npm install use-pull-to-refresh- Table of contents
- Description
- Purposes
- Prerequisites
- Getting Started
- What's the different to other similar packages?
- Installation
- Usage
- API
- Parameters
- Return Type
- Change log
- Authors
- License
A simple React custom hook for pull-to-refresh function that support NextJs SSR.
Lots of web application needs custom scroll area for various reasons, however since we are not using browser native scroll,
the native pull to refresh API is unusable since it's tied to the scroll event on html tag. This hooks is the solution to
resolve this issue by recreating the native pull top behavior while also let you choose what proccess to run on each pull
event end.
This project requires NodeJS (version 16 or later) and React 17+
This custom hook helps you implement the pull-to-refresh feature in your app. It supports NextJs SSR that some other packages don't. It also allows support of a custom Scroll Area that was created by yourself.
- react-pull-to-refresh : will run into error 'window' is not defined when using with NextJs SSR.
- react-hooks-pull-to-refresh, react-pull-updown-to-refresh, react-web-pull-to-refresh : Either being unmaintained or doesn't support Typescript, or newer version of React.
- react-native-pull-refresh-android : solution only available for react native.
BEFORE YOU INSTALL: please read the prerequisites
To install and set up the library, run:
``sh`
$ npm i use-pull-to-refresh
Or if you prefer using Yarn:
`sh`
$ yarn add use-pull-to-refresh
Or if you prefer using pnpm:
`sh`
$ pnpm add use-pull-to-refresh
`jsx
import { useRouter } from 'next/router';
import { usePullToRefresh } from 'use-pull-to-refresh';
const MAXIMUM_PULL_LENGTH = 240;
const REFRESH_THRESHOLD = 180;
export default function PageRefresh() {
const { isReady, reload } = useRouter();
const { isRefreshing, pullPosition } = usePullToRefresh({
// you can choose what behavior for onRefresh, could be calling an API to load more data, or refresh whole page.
onRefresh: reload,
maximumPullLength: MAXIMUM_PULL_LENGTH,
refreshThreshold: REFRESH_THRESHOLD,
isDisabled: !isReady
});
return (
}
style={!isRefreshing ? { transform: rotate(${pullPosition}deg) } : {}}
>
API
$3
`typescript
type UsePullToRefreshParams = {
onRefresh: () => void;
maximumPullLength?: number;
refreshThreshold?: number;
isDisabled?: boolean;
};
`-
onRefresh (required): refresh callback function run when pull event end.
- maximumPullLength: limit how far the refresh icon was pulled down.
- refreshThreshold: pullPosition that will trigger onRefresh function.
- isDisabled: disabling pull function in case the onRefresh function is not ready to run.$3
`typescript
type UsePullToRefreshReturn = {
isRefreshing: boolean;
pullPosition: number;
};
`-
isRefreshing: indicate refresh callback function is running.
- pullPosition`: current pull gesture position.- Senbonzakura1234 - _Initial work_ - Senbonzakura1234
MIT License © Senbonzakura1234