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.

Create new reusable id-based cache for CellMeasurer

See original GitHub issue

The default cache for CellMeasurer currently stores sizes using indices. It should be possible to store these values using (data) ids instead so that changes to the underlying data (eg sorting, new rows appended) wouldn’t invalidate the cached sizes. This would be useful for things like reverse-ordered lists (eg chat).

cc @zsherman

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:16
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
m0xxcommented, Dec 16, 2016

I had some performance issue with the CellMeasurer for highly dynamic row height. So i’ve come up with a simple solution(key based caching) that seemed to resolve also this issue.

I wanted to use the CellMeasurer to calculate the dynamic height but only when a similar row hasn’t been computed yet. For example let’s say that each row has 0…n child I could use the child count as my key and still benefits from the CellMeasurer to measure all my item + padding/margin.

If someone is interested here is my implementation:

export default class KeyBasedCellSizeCache {
  constructor ({
      buildColumnKey = (index) => (0),
      buildRowKey = (index) => (0)
    } = {}) {
    this._buildColumnKey = buildColumnKey;
    this._buildRowKey = buildRowKey;

    this._keyByRowIndex = {}
    this._keyByColumnIndex = {}

    this._heightByRowKey = {}
    this._widthByColKey = {}
  }

  clearAllColumnWidths () {
    this._keyByRowIndex = {}
  }

  clearAllRowHeights () {
    this._keyByRowIndex = {}
  }

  clearColumnWidth (index) {
    delete this._keyByColumnIndex[index]
  }

  clearRowHeight (index) {
    delete this._keyByRowIndex[index]
  }

  getColumnWidth (index) {
    return this._widthByColKey[this.getColumnKey(index)]
  }

  getRowHeight (index) {
    return this._heightByRowKey[this.getRowKey(index)]
  }

  setColumnWidth (index, width) {
    this._widthByColKey[this.getColumnKey(index)] = width
  }

  setRowHeight (index, height) {
    this._heightByRowKey[this.getRowKey(index)] = height
  }

  getRowKey(index) {
    if(!this._keyByRowIndex[index]) {
      this._keyByRowIndex[index] = this._buildRowKey(index)
    }

    return this._keyByRowIndex[index]
  }

  getColumnKey(index) {
    if(!this._keyByColumnIndex[index]) {
      this._keyByColumnIndex[index] = this._buildColumnKey(index)
    }

    return this._keyByColumnIndex[index]
  }
}

And thanks for this awesome library!

1reaction
zshermancommented, Dec 5, 2016

Yep, we’re using this strategy right now without CellMeasurer, just using our own cache and passing heights straight to List. Having this baked in would be awesome though…

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-virtualized share CellMeasurerCache for multiple Grids
You can't directly share a CellMeasurerCache cache between Grid s unless the content of all cells in both Grid s are the same...
Read more >
Create a Virtualized List with Auto Sizing Cells using react ...
In this lesson we'll use CellMeasurer and CellMeasurerCache to automatically calculate and cache the height of a row.
Read more >
How to use the react-virtualized/dist/commonjs/CellMeasurer ...
Use Snyk Code to scan source code in minutes - no build needed - and fix issues ... columnHeights = {}; this.cache =...
Read more >
react-easy-virtualized - npm
Latest version: 0.0.7, last published: 3 months ago. ... CellMeasurer } from 'react-virtualized'; const cache = new CellMeasurerCache(); ...
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