purii / react-use-scrollspy Goto Github PK
View Code? Open in Web Editor NEWFlexible React Hook to automatically update navigation based on scroll position
Home Page: https://purii.github.io/react-use-scrollspy/
License: MIT License
Flexible React Hook to automatically update navigation based on scroll position
Home Page: https://purii.github.io/react-use-scrollspy/
License: MIT License
If I install the package on a typescript project, I get a warning:
Could not find a declaration file for module 'react-use-scrollspy'.
A declaration file should be added for typescript users
First of all, thanks for the fantastic hook. Works like a charm! ๐
So, I have a page with dynamically generated sections with editable content, so the last section on the page sometimes has a small height say 100px
and doesn't trigger the hook like it should be for other sections on the page
The hook works perfectly, even for the last section on the page, if it's height is large enough to be covered into the threshold value calculated using the offsetPx
prop
import useScrollSpy from 'react-use-scrollspy';
// if I have 3 sections, and the height of the 3rd(last) section is small
// then activeSection value is never set to its index value i.e. 2
const activeSection = useScrollSpy({
sectionElementRefs: sectionRefsArray,
offsetPx: Y_OFFSET
});
Is there any way to handle this scenario?
Please add a useEffect call to the hook, so that the state is checked on re-renders. Especially on the first render. If you scroll to the last section and refresh the page the value is incorrect.
README.md refers to the project as react-use-scrollyspy
(note the y
after scroll
).
Could you kindly advice on how to get the activeSection
by sectionRefs id? Currently it is returning the index of the sectionRefs - 0, 1, 2 . I would need to return by sectionRefs.getAttribute(id)
because the order of the section might differ than the navigation.
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,
});
console.log(activeSection); ----> currently getting the index of the sectionRefs - 0, 1, 2 . Option to return by sectionRefs.getAttribute(id) because the order of the section might differ than the navigation.
return (
<section id="section-1" ref={sectionRefs[0]}>
<h1>Section 1</h1>
</section>
<section id="section-2" ref={sectionRefs[1]}>
<h1>Section 2</h1>
</section>
<section id="section-3" ref={sectionRefs[2]}>
<h1>Section 3</h1>
</section>
)
Any thoughts on what I can do if I have a variable number of sections to watch?
I can't run useRef in a loop (it's against the "rules of hooks").
Hi,
thanks for this hook. I was wondering if it would be possible to use it with a custom scroll container, e.g. a fixed height div with overflow: auto
containing my list of sections. If so a quick example would be appreciated.
Cheers
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.