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 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 :: i18n turn off suspense react 
Javascript :: jspdf line 
Javascript :: js nearest 100 
Javascript :: javascript get random array item 
Javascript :: object loop in javascript 
Javascript :: javascript leap year 
Javascript :: moment js date diff 
Javascript :: javascript date to utc format 
Javascript :: next + tailwind npm 
Javascript :: regex to check if string contains special characters javascript 
Javascript :: node mon in loopback 
Javascript :: does json only support ascii 
Javascript :: generate random date in a range 
Javascript :: how to send an embed message discord.js 
Javascript :: js detect scroll 
Javascript :: performance javascript 
Javascript :: react map 
Javascript :: popup alert in flutter 
Javascript :: get current url last part angular 
Javascript :: redirect not found in react-router-dom 
Javascript :: javascript separate words by capital letter 
Javascript :: Add event listener to multiple buttons with the same class 
Javascript :: obj[key].includes is not a function 
Javascript :: typing refs react 
Javascript :: javascript read xlsx file 
Javascript :: javascript to convert rgb to hsl 
Javascript :: jquery datatables turn off sorting 
Javascript :: javascript regex replace all 
Javascript :: javascript get text from paragraph 
Javascript :: select2 preselect option 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =