Image Preloader for React & React Virtualized
npm install react-virtualized-image-measurer``sh`
$ npm install react-virtualized-image-measurer --save-dev
https://codesandbox.io/s/7y66p25qv6
Component accepts an array of items, tries to extract an image from each item using image callback prop,children
then loads the image, measures it and provides the outcome to render-prop.
`js
const list = [
{
image: 'http://...',
title: 'Foo'
}
//...more
];
export default () => (
image={item => item.image}
defaultHeight={100}
defaultWidth={100}
>
{({itemsWithSizes, sizes}) => (
// itemsWithSizes = [{item: listItem, size: {width: x, height: x}]
// sizes = {'src': {width: x, height: x}}
)}
);
`
You can return custom width and height from onError callback prop. If nothing was returned defaultWidth anddefaultHeight will be used.
`js`
export default () => (
console.error('Cannot load image', src, 'for item', item, 'event', event);
return {width: 100, height: 100};
}}
>...
);
You should not do anything extra if you simply add items to the end of original array. But if you filter items, change
their order or insert items in the middle (basically anything that affect old items positioning), you have to manually
reset Masonry caches due to it's optimizations.
To do that you will have to save Masonry's ref somewhere:
`js`
const setRef = (node) => masonryRef = node;
And using this ref call following methods:
`js`
cellMeasurerCache.clearAll();
cellPositioner.reset(cellPositionerConfig);
masonryRef.clearCellPositions();
You can supply a custom key extractor callback prop in case you have duplicates in your array:
`js``
export default () => (
>...
);