Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react scroll to bottom of div

import React, { useEffect, useRef } from 'react'

const Messages = ({ messages }) => {

  const messagesEndRef = useRef(null)

  const scrollToBottom = () => {
    messagesEndRef.current?.scrollIntoView({ behavior: "smooth" })
  }

  useEffect(() => {
    scrollToBottom()
  }, [messages]);

  return (
    <div>
      {messages.map(message => <Message key={message.id} {...message} />)}
      <div ref={messagesEndRef} />
    </div>
  )
}
Comment

scroll to bottom of an element react

// without smooth-scroll
const scrollToBottom = () => {
		divRef.current.scrollTop = divRef.current.scrollHeight;
};

//with smooth-scroll
const scrollToBottomWithSmoothScroll = () => {
   divRef.current.scrollTo({
        top: divRef.current.scrollHeight,
        behavior: 'smooth',
      })
}

scrollToBottom()
scrollToBottomWithSmoothScroll()
Comment

react scroll to bottom

const scrollToBottom = () => {
	containerRef.current?.scrollToEnd()
};
Comment

scroll to bottom react

const messagesEndRef = useRef(null);
  const [msgs, setMsgs] = useState([]);

  const scrollToBottom = () => {
    messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
  };

  useEffect(() => {
    scrollToBottom();
  }, [msgs]);
Comment

PREVIOUS NEXT
Code Example
Javascript :: scroll to bottom of an element javascript 
Javascript :: form reset jquery 
Javascript :: jspdf line 
Javascript :: check whether a checkbox is checked in jQuery 
Javascript :: codemirror get value 
Javascript :: js add seconds to current time 
Javascript :: form serialize to json 
Javascript :: javascript get data attribute of selected option 
Javascript :: js separate number with comma 
Javascript :: how to get the end of an array javascript 
Javascript :: your mom is your dad 
Javascript :: loop through each class jq 
Javascript :: detect when page scroll to div javascript no jquery 
Javascript :: react index.js BrowserRouter 
Javascript :: javaScript getSeconds() Method 
Javascript :: check if character is a letter 
Javascript :: nodejs print variable in string 
Javascript :: split a string every n characters javascript 
Javascript :: legacy react start 
Javascript :: react absolute import 
Javascript :: go to nextelementsibling js 
Javascript :: stopped typing jquery 
Javascript :: js check if string is base64 
Javascript :: angular usehash not working 
Javascript :: save in json file js 
Javascript :: classlist has class 
Javascript :: event listener for element lost focus 
Javascript :: fetch api post req 
Javascript :: jquery remove background color 
Javascript :: retrieve object array value based on key 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =