Next.js Discord

Discord Forum

Struggling with the document scroll event

Unanswered
Pacific sandlance posted this in #help-forum
Open in Discord
Avatar
Pacific sandlanceOP
Yo, im trying to attach a scroll listener to a div element but its not firing

4 Replies

Avatar
Pacific sandlanceOP
This is my snippet of the relevant code:
useEffect(() => {
    const scrollContainer = scrollRef.current;
    if (!scrollContainer) return;
    scrollContainer.addEventListener("scroll", onScroll);
    return () => {
      if (scrollContainer) {
        scrollContainer.removeEventListener("scroll", onScroll);
      }
    };
  }, [modalOpen]);




{player && modalOpen ? (
        <>
.......
<div
                        style={{
                          maxHeight: "300px",
                          overflowY: "auto",
                        }}
                        ref={scrollRef}
                      >
                        <div dangerouslySetInnerHTML={{__html: applyConfigProfile.requirements}} />
                      </div>
.......

Problem is for some reason the ref is always null
Avatar
Pacific sandlanceOP
bump
Avatar
Pacific sandlanceOP
bump
Avatar
Pacific sandlanceOP
i fixed it with the onScroll event