Next.js Discord

Discord Forum

Struggling with the document scroll event

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

4 Replies

Avatar
ginOP
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
ginOP
bump
Avatar
ginOP
bump
Avatar
ginOP
i fixed it with the onScroll event