Is there a way to set a ref to List with react-virtualized?

Viewed 1066

I'm trying to reference the scrollable List in react-virtualized by using a ref. But my ref is always showing it's current attribute as undefined. Does anyone know how to use a ref with react-virtualized List?

Here's what I am currently doing, but listRef.current is always undefined:

const listRef = useRef()

<List
      width={350}
      height={400}
      rowCount={listsprings.length}
      rowHeight={100}
      rowRenderer={rowRenderer}
      style={{ outline: 'none' }}
      ref={listRef}
      />
2 Answers

You must use pass callback since it uses LegacyRef.

const listRef = useRef()

<List
      ref={(ref) => { listRef.current = ref }}
      width={350}
      height={400}
      rowCount={listsprings.length}
      rowHeight={100}
      rowRenderer={rowRenderer}
      style={{ outline: 'none' }}

/>

when you use ref, you should care for "listRef.current" but not just "listRef"

Related