Skip to content

Flexible React Hook to automatically update navigation based on scroll position

License

Notifications You must be signed in to change notification settings

exil0867/react-use-scrollspy

 
 

Repository files navigation

react-use-scrollspy

Build Status npm version npm GitHub license Donate

Example

Installation

react-use-scrollspy is a React Hook which requires React 16.8.0 or later.

// yarn
yarn add react-use-scrollspy
// or npm
npm i react-use-scrollspy --S

Usage

import useScrollSpy from 'react-use-scrollspy';
...
const activeSection = useScrollSpy({
  sectionElementRefs: [], // Array of References to DOM elements
});
Parameter Default Type Description
defaultValue 0 int Default value that is returned (optional)
offsetPx 0 int Set offset (optional)
sectionElementRefs [] [Ref] Array of Refs to observe (e.g. via React refs)

with Refs

Use React refs for section elements like in the provided example.

Fire up the demo and develop

Install dependencies and build a tgz file for the demo to use as a local package:

npm i && npm run build && npm pack

Move the package file to the bin directory:

mkdir -p ./example/bin && mv *.tgz ./example/bin

install the demo dependencies:

cd example && npm i

Start the demo:

npm start

About

Flexible React Hook to automatically update navigation based on scroll position

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 33.1%
  • TypeScript 32.5%
  • Dockerfile 23.2%
  • HTML 7.3%
  • CSS 3.9%