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.

[Flatlist] initialScrollIndex doesn't work at all

See original GitHub issue

Is this a bug report?

Yes

Have you read the Bugs section of the Contributing to React Native Guide?

Yes

Environment

react-native: 0.45.1

Steps to Reproduce

(Write your steps here:)

Made a horizontal flatlist with initialScrollIndex. It should go to the index and stay there.

<FlatList
        data={list}
        disableVisualisation="false"
        keyExtractor={this.keyExtractor}
        renderItem={this.renderItems}
        horizontal={true}
        scrollEventThrottle={2000}
        pagingEnabled={true}
        initialScrollIndex={8}
        showsHorizontalScrollIndicator={false}
        onScroll={this.scroll}
        getItemLayout={this.layout}
        initialNumToRender={3}
      />

Actual Behavior

It goes to the right index and after about a half second it goes back to the first item in the list.

No idea how to implement it, because method scrollToIndex didn’t work too.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
DevSzurmanskicommented, Jul 12, 2017

@hramos @jstheoriginal I resolved my problem - my getItemLayout was wrong, but now I have similar issue. It goes to the good index, then I’t blank for about 0.5s and then it’s rerendering the item on the good index. My component doesn’t rerender. OnScroll and scrollEventThrottle works and they have no affect on my issue. InitialNumToRender didn’t helped too even with higher number than initialScrollIndex.

const widthScreen = Dimensions.get("window").width;
class HorizontalArticlesListView extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      list: [],
    };
  }
  componentWillMount() {
    let { data, id, hotspotTop, hotspotBottom } = this.props.navigation.state.params;
    this.setState({ list: [hotspotTop, hotspotBottom, ...data] });
  }
  renderItems = ({ item }) => {
    return (
      <ArticleTypeHOC id={item.type + item.id} article={item} navigation={this.props.navigation} />
    );
  };

  keyExtractor = (article, index) => article.url;

  layout = (data, index) => ({ length: widthScreen, offset: widthScreen * index, index });

  render(): JSX.JSXElement {
    return (
      <FlatList
        data={this.state.list}
        disableVisualisation={false}
        keyExtractor={this.keyExtractor}
        renderItem={this.renderItems}
        horizontal={true}
        scrollEventThrottle={250}
        pagingEnabled={true}
        initialScrollIndex={9}
        showsHorizontalScrollIndicator={false}
        onScroll={this.scroll}
        getItemLayout={this.layout}
        initialNumToRender={1}
      />
    );
  }
}
export default HorizontalArticlesListView;
1reaction
chinalwbcommented, Jul 19, 2018

@pcguru Could you give me some pointer about how to ‘Precompute the offset’ if each item has a different height? (my app is similar to the IM app, the item’s height depends on the comment’s length)

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

initialScrollIndex not working for FlatList react native
I've solved this with setting: onContentSizeChange which will handle scroll to index whenever flatlist data is changed and all items rendered ( ...
Read more >
FlatList issue with initialScrollIndex - Expo Snack
FlatList issue with initialScrollIndex. No description. Open with Expo Go. Open in editor. Need Expo? Don't have the Expo Go? Download the app...
Read more >
initialScrollIndex not working for FlatList react native-React ...
Coding example for the question initialScrollIndex not working for FlatList react native-React Native.
Read more >
FlatList - React Native
Instead of starting at the top with the first item, start at initialScrollIndex . This disables the "scroll to top" optimization that keeps...
Read more >
FlatList initialScrollIndex Prop Not Working Properly Solution
If any of you know why it is not working then please share your opinion or code with me on comment section. It...
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