Skip to content

scrollToLocation in componentDidMount prevents SectionList rendering #21846

Closed
@hughbe

Description

@hughbe

Environment

Scanning folders for symlinks in /Users/hugh/Documents/GitHub/Oxford-Union/App/node_modules (31ms)

Environment:
  OS: macOS High Sierra 10.13.6
  Node: 8.9.4
  Yarn: Not Found
  npm: 6.4.1
  Watchman: Not Found
  Xcode: Xcode 10.0 Build version 10A255
  Android Studio: Not Found

Packages: (wanted => installed)
  react: 16.5.2 => 16.5.2
  react-native: https://github.com/expo/react-native/archive/sdk-30.0.0.tar.gz => 0.55.4

Description

I have a SectionList that contains a chronological list of events on a specific date. I am trying to set the initial scroll position to the current date. This is done by calling this.scrollView.scrollToLocation from a function called in componentDidMount.

In EventsPage.js

    scrollToCurrentDate = async () => {
        const { firstLoad, events } = this.state;
        if (!firstLoad) {
            return;
        }

        this.setState({firstLoad: false});

        // Get the section closest to today's date.
        var lastSectionIndex = -1;
        for (var i = 0; i < events.length; i++) {
            const section = events[i];
            if (new Date(section.date) > new Date()) {
                break;
            }

            lastSectionIndex = i;
        }

        if (lastSectionIndex === -1) {
            return;
        }

        const lastSection = events[lastSectionIndex];
        const hasCurrent = lastSection !== events[events.length - 1] && lastSection.date < new Date();
        if (!hasCurrent) {
            return;
        }

        this.scrollView.scrollToLocation({sectionIndex: lastSectionIndex, itemIndex: 0, animated: false});
    }

However, on first load, I get a blank screen - rendering has failed!

Expected:
image

Actual:
unadjustednonraw_thumb_afc

Workaround

Add this.scrollView.flashScrollIndicators(); after loading the screen!

Reproducible Demo

Please comment below and I'll allow access to the private reproducible example on github.

Setup

  1. Clone https://github.com/hughbe/oxford-union/tree/react-native-repro, follow the Setup instructions
  2. npm install -g expo-cli
  3. npm install
  4. npm start

Steps to reproduce

  1. Open the app and observe that the screen is ALL WHITE.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions