goldenyz / react-perfect-scrollbar

A react wrapper for perfect-scrollbar
MIT License
482 stars 92 forks source link

Scroll should start from bottom as default instead of top | react hooks #151

Open adnankarim opened 3 years ago

adnankarim commented 3 years ago

Scroll should start from bottom as default instead of top in react hooks components

Creskendoll commented 3 years ago

I don't think this library will be updated ever again.

Here's my solution derived from this SO answer.

import React, { useEffect, useState } from 'react';
import PerfectScrollbar from 'react-perfect-scrollbar';

const Messages = () => {
    const [scrollEl, setScrollEl] = useState();

    useEffect(() => {
      if (scrollEl) {
        // Set the scroll position to the bottom
        scrollEl.scrollTop = scrollEl.scrollHeight;
      }
    }, [scrollEl]);

    return (
        <PerfectScrollbar containerRef={setScrollEl}>
            {/* ............. */}
        </PerfectScrollbar>
  );
};

export default Messages;