Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

scrollbar automatically scroll down as new divs are added reactjs

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

PREVIOUS NEXT
Code Example
Javascript :: rect p5js 
Javascript :: redirect to 404 page in node js express 
Javascript :: Cast to ObjectId failed for value 
Javascript :: javascript get nth element of array 
Javascript :: reverse a number in javascript 
Javascript :: ajax load document ready 
Javascript :: btn.addeventlistener 
Javascript :: dom element get attribute 
Javascript :: add class when element in viewport vanilla javascript 
Javascript :: count number of duplicate pairs in array javascript 
Javascript :: how to use media queries in emotion 
Javascript :: jquery get name attribute 
Javascript :: how to make proptypes either or 
Javascript :: how to hide javascript code 
Javascript :: nuxt looks for npm_modules on express 
Javascript :: sample json 
Javascript :: mui switch colours 
Javascript :: how to get selected value of dynamically created dropdown in jquery 
Javascript :: javascript - get the filename and extension from input type=file 
Javascript :: npm yarn run shell script 
Javascript :: unrecognized font family fontawesome react native ios 
Javascript :: cancel button in react js 
Javascript :: check if an array is empty javascript 
Javascript :: find last element in array javascript 
Javascript :: column.footer jquery 
Javascript :: js input type range on hover 
Javascript :: momentTimeZone 
Javascript :: how to disable onclick event in javascript 
Javascript :: create react portal 
Javascript :: joi validation compare two password 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =