petyosi / react-virtuoso

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

[UNSOLVED][QUESTION] How to easly access to the data of a row ? #700

Closed MGMehdi closed 2 years ago

MGMehdi commented 2 years ago

Discussed in https://github.com/petyosi/react-virtuoso/discussions/683

Originally posted by **MGMehdi** June 14, 2022 Hello, If I want to access to the data of a row I need to do this ```js TableRow: React.forwardRef((props) => ( console.log(props.children.props.children[0].props.children)}/> )), ``` Isn't there a better solution ?
MGMehdi commented 2 years ago

Well, thanks I guess ?

adirzoari commented 1 year ago

@MGMehdi @petyosi did you find a solution? I'm trying to make hover to row and call an action when hover

AdamAtFoyer commented 1 year ago

One way to retrieve the row's data is to use the "context", and get the index of the row using a data attribute:

    TableRow: React.forwardRef((props, ref) => {
        const { context: { data, }, ...otherProps } = props;
        const index = props['data-index'];
        const item = data[index];

        return (
            <TableRow {...otherProps} />
        );
    })

And in your virtuoso table:

            <TableVirtuoso
                context={{ data, }}
                data={data}
                itemContent={renderRow} />