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.

After refreshing the FlatList, always set the contentOffset to 0

See original GitHub issue

šŸ› Bug Report

I’m using the FlatList with contentInset and contentOffset prop to guarantee the content doesn’t be hidden by header things. However, if I invoke some pull-to-refresh on the FlatList, contentOffset always goes to 0 after refreshing is finished. I think contentOffset should not be always zero if contentInset is already set.

I think that this might be the same as #20610 and I tested this is reproduced on the latest RN version.

To Reproduce

In the RNTester/js/FlatListExample.js, I added some code for refreshing, contnetInset, contentOffset` like below.

  [...] 
  componentDidMount() {
    // To make refreshing, I set the refreshing to be true right after componentDidMount
    // and set to false again after 3 secs
    this.setState({ refreshing: true });
    setTimeout(() => this.setState({ refreshing: false }), 3000);
  }
  [...]
  render() {
        [...]          
        <Animated.FlatList
           [...]
            contentInset={{ top: 50 }}
            contentOffset={{ y: -50 }}
            refreshing={this.state.refreshing} 
        />
  }
[...]

Expected Behavior

Here’s a screenshot when I only set the contentInset and contentOffset and didn’t call refreshing(just gave refreshing prop as false). I drew a red box which indicates contentInset and contentOffset works well. image

In this case, I think after refreshing that the same contentOffset should be maintained. However, contentOffset always goes to 0 as shown in below gif. Mar-28-2019 00-49-09

Code Example

Please see the To Reproduce section.

Environment

info React Native Environment Info: System: OS: macOS 10.14.3 CPU: (4) x64 IntelĀ® Coreā„¢ i5-7267U CPU @ 3.10GHz Memory: 137.07 MB / 16.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 8.15.1 - ~/.nvm/versions/node/v8.15.1/bin/node Yarn: 1.12.3 - /usr/local/bin/yarn npm: 6.4.1 - ~/.nvm/versions/node/v8.15.1/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman SDKs: iOS SDK: Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1 Android SDK: API Levels: 27, 28 Build Tools: 27.0.3, 28.0.2, 28.0.3 System Images: android-27 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom IDEs: Android Studio: 3.2 AI-181.5540.7.32.5056338 Xcode: 10.1/10B61 - /usr/bin/xcodebuild npmPackages: react: 16.8.3 => 16.8.3 react-native: 0.59.2 => 0.59.2 npmGlobalPackages: react-native-cli: 2.0.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
acro5pianocommented, May 14, 2019

Maybe this issue is related to #17734.

Now #17734 is closed, so I will post my solution here.

<FlatList
  contentOffset={{ y: this.props.loading ? -60 : 0, x: 0 }}  // <-- if loading, set offset
  // ...

Hope #24170 will fix it.

0reactions
acro5pianocommented, Jul 26, 2019

For me, upgrading to latest v0.59 and removing all offset value fixed the issue.

And I gave up to use loading props by always set it false, and place my original loading component over the FlatList while loading as the current workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I lock contentOffset y position of a FlatList when new ...
The trick is to scroll to the Y location (unanimated) when an item is added to the list function itemAdded() { //... const...
Read more >
FlatList - React Native
FlatList. A performant interface for rendering basic, flat lists, supporting the most handy features: Fully cross-platform.
Read more >
React Native Reanimated 2 - Collapsing Header + FlatList Tips
Now, this will make sure that translationY will always start at 0 even if we have contentOffset set to a different value on...
Read more >
Common bugs in React Native ScrollView and how to fix them
contentOffset – This value is the distance that the user has already scrolled from the beginning, most often used when we want to...
Read more >
Building a Smooth Image Carousel with FlatList in React Native
Since <FlatList /> only stores in memory the items that are about to appear ... Hide the horizontal scroll indicator, and set a...
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