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 javascript 
Javascript :: i18n turn off suspense react 
Javascript :: javascript context arc set color 
Javascript :: input pattern for np whitespaces at the end or beginning 
Javascript :: ace get contents of editor 
Javascript :: external linking of JavaScript in html 
Javascript :: jquery get div height 
Javascript :: jquery loop over elements 
Javascript :: regex to extract a phone number with country code 
Javascript :: generate random otp in node js 
Javascript :: loopback model properties 
Javascript :: create text editor with react-redux 
Javascript :: ascii to hex js 
Javascript :: how to get the current date and time in javascript 
Javascript :: remover clase jquery 
Javascript :: add dev dependency yarn 
Javascript :: how to do a classname variable and string react 
Javascript :: confirm before leaving page javascript 
Javascript :: parse date do weekday 
Javascript :: jquery call dynamically created class 
Javascript :: nextelementsibling js 
Javascript :: environment varriables with vite 
Javascript :: settimeout function 
Javascript :: useeffect async not working 
Javascript :: cypress type force 
Javascript :: character to ascii in js 
Javascript :: js onchange paramteter sring 
Javascript :: jquery select element based on for attribute 
Javascript :: how to connect metamask wallet with js 
Javascript :: how can search in object in array 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =