Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
react-use-scrollspy
Advanced tools
Flexible React Hook to automatically update navigation based on scroll position
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
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 ) |
scrollingElement | window | Ref | Target of the scrolling (e.g. via React refs )) (optional) |
Use React refs
for section elements like in the provided example.
import React, { useRef } from 'react';
import useScrollSpy from 'react-use-scrollspy';
const App = () => {
const sectionRefs = [
useRef(null),
useRef(null),
useRef(null),
];
const activeSection = useScrollSpy({
sectionElementRefs: sectionRefs,
offsetPx: -80,
});
return (
<nav className="App-navigation">
<span className={activeSection === 0 ? "App-navigation-item App-navigation-item--active" : "App-navigation-item"}>Section 1</span>
<span className={activeSection === 1 ? "App-navigation-item App-navigation-item--active" : "App-navigation-item"}>Section 2</span>
<span className={activeSection === 2 ? "App-navigation-item App-navigation-item--active" : "App-navigation-item"}>Section 3</span>
</nav>
<section className="App-section" ref={sectionRefs[0]}>
<h1>Section 1</h1>
</section>
<section className="App-section" ref={sectionRefs[1]}>
<h1>Section 2</h1>
</section>
<section className="App-section" ref={sectionRefs[2]}>
<h1>Section 3</h1>
</section>
)
FAQs
Flexible React Hook to automatically update navigation based on scroll position
We found that react-use-scrollspy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.