Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to scroll to the bottom in FlashList component? React Native

How to scroll to the bottom in FlashList component? React Native? For example when screen opens it automaticity reach to end of the List?

import React from 'react';
import { FlashList} from '@shopify/flash-list';

const MyComponent = ({route}) => {

   const { chat } = route.params;

  return (
    <FlatList
      data={chat}
      renderItem={renderItem}
      keyExtractor={keyExtractor}
    />
  );
};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you would like to automatically scroll to the bottom of your FlatList in React Native when your screen opens, you could use a combination of the ref prop and the scrollToEnd method.

import React, { useEffect, useRef } from 'react';
import { FlatList } from 'react-native';

const MyComponent = ({route}) => {
  const { chat } = route.params;

  const flatListRef = useRef(); // Create a ref object

  // The function renderItem() and keyExtractor() need to be defined for the FlatList
  const renderItem = ({ item }) => (
    // Your component for rendering each item
  );

  const keyExtractor = (item, index) => index.toString();

  useEffect(() => {
    setTimeout(() => {
      flatListRef.current.scrollToEnd(); // Scroll to the end of the list
    }, 0); // Use 0 for instant scroll
  }, []); // Only run once when the component mounts

  return (
    <FlatList
      ref={flatListRef} 
      data={chat}
      renderItem={renderItem}
      keyExtractor={keyExtractor}
    />
  );
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading