Skip to content

Commit c641e37

Browse files
author
Brian Vaughn
committed
Improved CellMeasurer docs by demonstrating use of idCellMeasurerCellSizeCache (see bvaughn#562).
1 parent 4a2806a commit c641e37

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

docs/CellMeasurer.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CellSizeCache {
5353
The [default caching strategy](https://github.com/bvaughn/react-virtualized/blob/master/source/CellMeasurer/defaultCellSizeCache.js) is exported as `defaultCellMeasurerCellSizeCache` should you wish to decorate it.
5454
You can also pass `uniformRowHeight` and/or `uniformColumnWidth` named parameters to the constructor for lists with a uniform (yet unknown) cell sizes.
5555

56-
An [id-based caching strategy](https://github.com/bvaughn/react-virtualized/blob/master/source/CellMeasurer/idCellSizeCache.js) is also available for data that may be sorted.
56+
An [id-based caching strategy](id-based-cell-size-cache) is also available for data that may be sorted.
5757
This strategy maps data ids to cell sizes rathe than index so that the sorting order of the data does not invalidate sizes.
5858

5959
### Examples
@@ -63,7 +63,7 @@ This strategy maps data ids to cell sizes rathe than index so that the sorting o
6363
This example shows a `Grid` with fixed row heights and dynamic column widths.
6464
For more examples check out the component [demo page](https://bvaughn.github.io/react-virtualized/#/components/CellMeasurer).
6565

66-
```javascript
66+
```jsx
6767
import React from 'react';
6868
import ReactDOM from 'react-dom';
6969
import { CellMeasurer, Grid } from 'react-virtualized';
@@ -92,6 +92,43 @@ ReactDOM.render(
9292
);
9393
```
9494

95+
#### ID-based cell size cache
96+
97+
`CellMeasurer` measures each cell once and then caches the measurements so it doesn't have to measure it again.
98+
By default this caching is done using the cell's row and column indices.
99+
Certain things (eg insertions, sorting) can invalidate this type of cache though.
100+
If your list is dynamic- you may consider using an id-based caching strategy instead.
101+
The `idCellMeasurerCellSizeCache` exists for this purpose:
102+
103+
```jsx
104+
import React from 'react';
105+
import ReactDOM from 'react-dom';
106+
import { CellMeasurer, Grid, idCellMeasurerCellSizeCache } from 'react-virtualized';
107+
import 'react-virtualized/styles.css'; // only needs to be imported once
108+
109+
ReactDOM.render(
110+
<CellMeasurer
111+
cellRenderer={cellRenderer}
112+
cellSizeCache={idCellMeasurerCellSizeCache}
113+
columnCount={columnCount}
114+
rowCount={rowCount}
115+
>
116+
{({ getColumnWidth, getRowHeight }) => (
117+
<Grid
118+
columnCount={columnCount}
119+
columnWidth={getColumnWidth}
120+
height={height}
121+
cellRenderer={cellRenderer}
122+
rowCount={rowCount}
123+
rowHeight={getRowHeight}
124+
width={width}
125+
/>
126+
)}
127+
</CellMeasurer>,
128+
document.getElementById('example')
129+
);
130+
```
131+
95132
###### Customizing `cellSizeCache`
96133

97134
The cell size cache can be optimized when width and/or height is uniform across cells.

0 commit comments

Comments
 (0)