react auto scroll with mouse code example
Example 1: 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>
)
}
Example 2: react scrolling a div by dragging the mouse?
import React from 'react';import classNames from 'classnames';import PropTypes from 'prop-types';export class Timeline extends React.PureComponent { render() { const { classes, data, } = this.props; return ( <div ref={this.ref}> <Timeline data={data} /> </div> ); }}Timeline.propTypes = { data: PropTypes.array,};export default Timeline;