question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android][Flatlist][Horizontal] Last item delete in horizontal flatlist does not adjust scroll position.

See original GitHub issue

React Native version: 0.59.10

output

Steps To Reproduce

export default class App extends Component<Props> {

  constructor(props) {
    super(props);
    this.dataItems = [{
      id: 1,
      text: 'one'
    },{
      id: 2,
      text: 'two'
    },{
      id: 3,
      text: 'three'
    }, {
      id: 4,
      text: 'four',
    }, {
      id: 5,
      text: 'five'
    }];
  }

  removeItem = (id) => {
    this.dataItems = this.dataItems.filter((item) => item.id !== id);
    this.setState({});
  };

  renderItem = ({item}) => {
    return (
      <View style={{ width: 140, height: 140, backgroundColor: 'yellow', margin: 10}}>
        <Text>{item.text}</Text>
        <TouchableOpacity onPress={() => {
          this.removeItem(item.id);
        }}>
          <Text>{'Remove me'}</Text>
        </TouchableOpacity>
      </View>
    )
  };

  render() {
    return (
      <View style={styles.container}>
        <View style={{height: 140, width: '100%'}}>
          <FlatList
            horizontal
            data={this.dataItems}
            renderItem={this.renderItem}
            keyExtractor={item => `${item.id}`}
          />
        </View>
      </View>
    );
  }
}

Describe what you expected to happen: After removing last item, scroll position should be adjusted.

Snack, code example, screenshot, or link to a repository:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:19

github_iconTop GitHub Comments

11reactions
victoralmeidadevcommented, Jul 26, 2021

Same problem here.

A workaround while waiting fix: use scrollToEnd when removing the last item.

Example:

import React, { useState, useRef, useCallback } from "react";
import { FlatList } from "react-native";

function ExampleComponent() {
  const [items, setItems] = useState<[]>([]);
  const flatListRef = useRef<FlatList>();

  const handleRemoveFlatListItem = useCallback(
    (index: number) => {
      const canScrollToEnd = index === items.length - 1;
      const newItems = [...items] as [];
      newItems.splice(index, 1);
      setItems([...newItems]);
      if (flatListRef?.current && canScrollToEnd)
        flatListRef.current.scrollToEnd();
    },
    [items, flatListRef]
  );

  const renderItem = ...

  return (
    <FlatList
      horizontal
      data={items}
      renderItem={renderItem}
      keyExtractor={(item) => `${item.id}`}
    />
  );
}
2reactions
jayshah123commented, May 26, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native FlatList not scrolling to end - Stack Overflow
I just passed a view with the preferable height and it worked for me perfectly. Here is the code snippet: <FlatList data={DATA} renderItem={({ ......
Read more >
Common bugs in React Native ScrollView and how to fix them
After putting all those elements inside the ScrollView component, you can use it to scroll through them vertically (the default) or horizontally ......
Read more >
ScrollView - React Native
FlatList renders items lazily, when they are about to appear, ... the scroll view bounces horizontally when it reaches the end even if...
Read more >
Building a Smooth Image Carousel with FlatList in React Native
Users can scroll through items by dragging across right or left to ... the end of the carousel, to help position the first...
Read more >
React Native scrollToIndex - Dynamic size item scroll inside ...
In this video tutorial you will learn about React Native scrollToIndex and how to scroll to an item inside a FlatList, ListView, ScrollView, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found