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 :: datatable default sorting 
Javascript :: how to add a right click listener javascript 
Javascript :: find intersection of multiple arrays in javascript 
Javascript :: js letters alphabet array 
Javascript :: make circle html canvas 
Javascript :: javascript base64 encode 
Javascript :: listen prop change vuejs 
Javascript :: javascript how to get a random element from an array 
Javascript :: vimeo regez video id 
Javascript :: wordpress echo admin ajax url 
Javascript :: generate random ip address javascript 
Javascript :: javascript string into substrings of length 
Javascript :: delegate click in jquery 
Javascript :: check if array javascript 
Javascript :: regex a-z javascript 
Javascript :: readable date in javascript 
Javascript :: how to wait foreach javascript 
Javascript :: javascript readfile 
Javascript :: three.js create sphere 
Javascript :: javascript add required attribute to input 
Javascript :: getting the current url in node js 
Javascript :: expo build apk 
Javascript :: Masonry js cdn 
Javascript :: process.now() nodejs 
Javascript :: javascript format number as currency 
Javascript :: wordpress header script add 
Javascript :: sequelize generate migration 
Javascript :: mongoose timestamps 
Javascript :: ngmodeloptions standalone 
Javascript :: jquery scroll to id 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =