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 :: on click copy text 
Javascript :: javascript round number to 5 decimal places 
Javascript :: tailwind content for nextjs 
Javascript :: vue scroll div to bottom 
Javascript :: how to convert set to a string in js 
Javascript :: javascript remove query string from url 
Javascript :: how to normalize string in javascript 
Javascript :: mongoose unique validator 
Javascript :: js math function that returns smallest value 
Javascript :: remove undefined from object js 
Javascript :: for loop string array javascript 
Javascript :: sfc in react 
Javascript :: js loading spinner 
Javascript :: mongodb find all that dont have property 
Javascript :: how to add number in string in javascript 
Javascript :: how to make fake binary 
Javascript :: javascript dump strack trace 
Javascript :: get id of an element 
Javascript :: getDataSnapshotFirebase 
Javascript :: for each loop with arrowfunction 
Javascript :: add json object to json array javascript 
Javascript :: how to clear node modules folder from your computer 
Javascript :: add eslint dependencies for cypress 
Javascript :: node js and react js difference 
Javascript :: generate random color 
Javascript :: foreach in javascript skip first 
Javascript :: react native new project mac 
Javascript :: lodash remove multiple items from array 
Javascript :: react context 
Javascript :: getting empty req.body 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =