React: [useRef, scrollIntoView,] How to only autoscroll a specific div inside of an overflowing page?

bauervision picture bauervision · Feb 18, 2020 · Viewed 7.9k times · Source

As you can see in my example

The page loads, and both the page and the nested scrolling div auto scroll.

What I'm trying to do is only scrollIntoView the overflowing div I have set as my ref.

I'm targetting the ref, and it is working, but how do I NOT scroll the parent element as well?

Answer

NirG picture NirG · Feb 18, 2020

Try this:

   messagesRef.current.scrollIntoView({
      behavior: "smooth",
      block: "nearest",
      inline: "start"
    });