4

I was looking for a way to know which list item is visible on the screen when using react-window .

The isVisible prop is returning the visibility wrongly .

https://codesandbox.io/s/bvaughnreact-window-fixed-size-list-vertical-nmukq

import React from "react";
import { render } from "react-dom";
import { FixedSizeList as List } from "react-window";
import AutoSizer from "react-virtualized-auto-sizer";
import TrackVisibility from "react-on-screen";

import "./styles.css";

const Row = ({ index, style, isVisible }) => (
  <div className={index % 2 ? "ListItemOdd" : "ListItemEven"} style={style}>
    Row {index + " is" + isVisible}
  </div>
);
const RowWrapper = ({ index, style }) => (
  <TrackVisibility>
    <Row index={index} style={style} />
  </TrackVisibility>
);

const Example = () => (
  <AutoSizer>
    {({ height, width }) => (
      <List
        className="List"
        height={height}
        itemCount={1000}
        itemSize={35}
        width={width}
      >
        {RowWrapper}
      </List>
    )}
  </AutoSizer>
);

render(<Example />, document.getElementById("root"));

This could possibly be because of caching of items, but i was wondering if there is another way to track visibility of an item

charan
  • 71
  • 6
  • I was able to get the visible items by listening to onwheel on outerElementType and doing some math . But this is all for fixedsize , not sure how to do it for dynamicsizelist . – charan Jun 29 '19 at 03:01

1 Answers1

3

I figured it out on my own . Please find the code sandboxes at

DynamicSizeList - https://codesandbox.io/s/piecykreact-window-dynamic-size-list-vertical-pooy2

By trapping the onWheel event and comparing the "tops" with clientHeight and ListHeight

FixedSizeList -https://codesandbox.io/s/bvaughnreact-window-fixed-size-list-vertical-nmukq

Also based on onWheel event.

It doesnt work with react-on-screen .

If someOne knows a better way of doing then please answer.

charan
  • 71
  • 6