@nfront/use-scroll-position

1.1.1 • Public • Published

Build Status Greenkeeper badge Maintainability Test Coverage npm bundle size npm (scoped)

Twitter URL Twitter Follow

@nfront/use-scroll-position (useScrollPosition)

useScrollPosition is a React hook that returns the browser window's X and Y scroll position.

This package uses a modern and stable Observable implementation.

Install

npm install --save @nfront/use-scroll-position

or:

yarn add @nfront/use-scroll-position

Why to use

use-scroll-position returns the scroll position of the browser window, using a modern, stable and performant implementation.

Unlike other packages with similar purpose, this package relies on the robust and modern reactive library RxJS.

This hook only updates state when a scroll event happens, and does so at the interval specifified with the throttle variable (see below).

How to use

Syntax:

const scrollPosition = useScrollPosition( throttle );

Full example of using the hook in a React component:

import useScrollPosition from 'use-scroll-position';
const MyComponent = props => {
  const scrollPosition = useScrollPosition(100);
  console.log(scrollPosition);
};

Options

  • throttle: (integer) Pass in the ms value you want the scroll-checker to throttle for. For instance, if you pass in 1000, the hook will only produce scroll values every 1 seconds. We recommend 100 for normal usage, which will produce scroll values every 100th millisecond.

Package Sidebar

Install

npm i @nfront/use-scroll-position

Weekly Downloads

2

Version

1.1.1

License

ISC

Unpacked Size

12.1 kB

Total Files

9

Last publish

Collaborators

  • magnusga