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.

List doesn't update display or row heights when data updated.

See original GitHub issue

Setup I have this List that uses CellMeasurer to compute row heights dynamically.

<WindowScroller ref={ref => (this.windowScroller = ref)}>
  {({ height, isScrolling, onChildScroll, scrollTop }) => (
    <AutoSizer disableHeight={true}>
      {({ width }) => (
        <div>
          <List
            autoHeight={true}
            height={height}
            isScrolling={isScrolling}
            onScroll={onChildScroll}
            scrollTop={scrollTop}
            rowCount={contacts.length}
            rowHeight={this._cache.rowHeight}
            rowRenderer={this._rowRenderer}
            width={width}
            deferredMeasurementCache={this._cache}
            overscanRowCount={0}
          />
        </div>
      )}
    </AutoSizer>
  )}
</WindowScroller>

Problem The list doesn’t update either display or row heights when data has changed. For the list to change, I have to scroll the window manually. I’ve tried to clear the cache, updatePosition and forceRender but still has no effect.

Temporary Solution Since scrolling to update isn’t acceptable, I created a hack in the componentDidUpdate to be able to update the list when data changes.

/**
   * Since virtual list doesn't update till it scrolls, programmatically scroll window to
   * trigger update.
   */
  public componentDidUpdate(prevProps: Props) {
    const becameVisible = this.props.isVisible && !prevProps.isVisible;
    const contactChanged = this.props.contacts !== prevProps.contacts;

    if (this.windowScroller && (becameVisible || contactChanged)) {
      this._cache.clearAll();
      this.windowScroller!.updatePosition();

      window.scrollTo(window.scrollX, window.scrollY + 5);
      window.scrollTo(window.scrollX, window.scrollY - 5);
    }
  }

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:14
  • Comments:11

github_iconTop GitHub Comments

14reactions
goldyluckscommented, Aug 4, 2019

for me this didn’t work, so I dynamically set an “artificial” key on the List, and I bump it by 1 to programmatically force a rerender

class MyComponent extends React.Component {
  this.listKey = 0

  componentDidUpdate() { this.listKey += 1 }
  ...

  render() {
    <List key={this.listKey} some more props here />
  }
}
13reactions
superman66commented, Jan 21, 2019

You can add ref

       <List
           ref={this.bindListRef}
            autoHeight={true}
            height={height}
            isScrolling={isScrolling}
            onScroll={onChildScroll}
            scrollTop={scrollTop}
            rowCount={contacts.length}
            rowHeight={this._cache.rowHeight}
            rowRenderer={this._rowRenderer}
            width={width}
            deferredMeasurementCache={this._cache}
            overscanRowCount={0}
          />

the bindListRef function

  bindListRef = ref => {
    this.list = ref;
  };

At last, call forceUpdateGrid in ComponentDidUpdate

  componentDidUpdate() {
    if (this.list) {
      this.list.forceUpdateGrid();
    }
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic row heights with react-virtualized and new ...
I need to update the row heights when the list data has changed. The current CellMeasurer doc doesn't seem to have any info...
Read more >
Add row to data list does not expand panel | Justinmind Q&A
I have a data list in a dynamic panel. My hope was that as new rows are added to the data list, the...
Read more >
Change the column width and row height - Microsoft Support
Go to File > Options > Advanced > Display > select an option from the Ruler Units list. If you switch to Normal...
Read more >
How to change and AutoFit row height in Excel - Ablebits
When copying data into Excel sheets, there are times when a row height does not adjust automatically. As the result, multi-line or unusually ......
Read more >
Working with Data: Updating Data - AG Grid
Insert Row @ 2: Inserts a row at position 2 in the list. This works in the grid as it doesn't allow sorting,...
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