Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to create a table of content using this library? #683

Unanswered
colonder asked this question in Q&A
Discussion options

I'm trying to adapt the blog entry from OpenReplay but I have issues wrapping my head around the examples. This is how I modified useScrollSpy hook they wrote about:

import { useEffect, useState } from "react";
import { observe } from "react-intersection-observer";
export function useScrollSpy(
 ids: string[],
 options: IntersectionObserverInit
): string | undefined {
 const [activeId, setActiveId] = useState<string | undefined>();
 useEffect(() => {
 const elements = ids.map((id) => document.getElementById(id));
 elements.forEach((el) => {
 if (el) {
 const destroy = observe(
 el,
 (inView, entry) => setActiveId(entry.target.id),
 options
 );
 // destroy();
 }
 });
 }, [ids, options]);
 return activeId;
}

although I think there's a better way to do that. How can this be achieved? I found similar (I think) discussions here, here, here and here although I'm a rookie at React and I find it a bit confusing.

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /