petyosi / react-virtuoso

The most powerful virtual list component for React
https://virtuoso.dev
MIT License
5.25k stars 301 forks source link

QUESTION: It is possible to access data property through a ref #421

Closed adamvietnam closed 3 years ago

adamvietnam commented 3 years ago

If a parent passes a reference down to a child component containing a list i.e.

   <Virtuoso data={listItems}
                  ref={someRef}
  .
  .
  .
  <Virtuoso/>

Can the parent then access the data property using the ref in , for instance, an event handler i.e.

const parentOnClickHandler=(e) => {
    const itemsSelected=listRef.current.data.filter( (element,index) => {  
       return someRef.current.data[index].isSelected;
   });
}

At the moment, when I inspect the someRef.current I can see the API methods but not the API properties.

I'm using version 1.10.2 of react-virtuoso and my code is in Javascript rather than Typescript so I can't import <VirtuosoHandle>

I'm also pretty new to ReactJS so apologies if this is dumb question.

petyosi commented 3 years ago

No, such API is not exposed. IMO, doing something like this violates the unidirectional data flow principles in React.

adamvietnam commented 3 years ago

I understand and suspected that was the case.

I'll propagate upwards which I'm sure is more sensible.

Thank you for your prompt response.