Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.