0

I'm struggling with ensuring that the Segment is scrolled to the bottom. I read up a lot on Stack Overflow about how we have the scrollTop attribute in CSS as well as the scrollTop() function with jQuery. I'm copying here the relevant code I was able to find:

Heads up: I'm using React and Semantic UI react if that makes a difference. Also, I've just installed jQuery via npm to try to use the solutions I found online.

This is my Div:

<Segment  id="myDivID" style={{minHeight:"450px",maxHeight:"450px", "overflow-y":"auto", "scrollTop":"450px"}} >
    // all of the individual chat messages                                       
</Segment> 

and here is the styling I'm trying to use in addition to the scrollTop attribute in the style passed above.

useEffect(()=>{
            var element = document.getElementById("myDivID");
            if(element){
                    window.scrollTo(0,element.offsetHeight);

            }
    },[])
    $('#myDivID').scrollTop($('#myDivID').scrollHeight);
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Karan Bhasin
  • 237
  • 3
  • 13

1 Answers1

0

You must define the ref.

const messageRef = useRef();

To this defined ref, you should give it to your message div

<Segment  id="myDivID" style={{minHeight:"450px",maxHeight:"450px", "overflow-y":"auto", "scrollTop":"450px"}} >
   <div ref={messageRef}>
      // all of the individual chat messages
   </div>                                     
</Segment> 

Finally,

useEffect(() => {
    if (messageRef.current) {
      messageRef.current.scrollIntoView(
        {
          behavior: 'smooth',
          block: 'end',
          inline: 'nearest'
        })
    }
  })