table icon indicating copy to clipboard operation
table copied to clipboard

Can I use with class component?

Open huypvw opened this issue 6 years ago • 7 comments

How to use react-table with react component class?

huypvw avatar Dec 24 '19 10:12 huypvw

Yes, just wrap your usage of useTable (and any other hooks) in a functional component and then reference that functional component from your class component.

ggascoigne avatar Dec 25 '19 23:12 ggascoigne

I am using class-based components throughout my project. To use the react-table I did the same as suggested above. I created a function-based component using useTable hook and used the functional component in the class based component. Still I face the below problem. Hooks can only be called inside the body of a function component. (https://fb.me/react-invalid-hook-call)

Sujatha-Pushparaj avatar Feb 27 '20 16:02 Sujatha-Pushparaj

Has anyone been able to get this to work?

keithslater avatar Jul 31 '20 17:07 keithslater

I'm not...

AaronActu avatar Oct 13 '20 16:10 AaronActu

Could someone provide any example?

me also stopped on this issue

just few lines of code there react down on hooks issue (need to provide hooksand method for state controlling to a useTable instance constructor)

        // Use the state and functions returned from useTable to build your UI
            const {
                getTableProps,
                getTableBodyProps,
                headerGroups,
                prepareRow,
                page,
                canPreviousPage,
                canNextPage,
                pageOptions,
                pageCount,
                gotoPage,
                nextPage,
                previousPage,
                setPageSize,
                state: { pageIndex, pageSize }
            } = useTable({
                columns: ...........,
                data: [],
                defaultColumn: undefined,
                getRowId<D>(originalRow: D, relativeIndex: number, parent: Row<D> | undefined): string {
                    return "";
                },
                getSubRows<D>(originalRow: D, relativeIndex: number): D[] {
                    return [];
                },
                initialState: undefined,
                stateReducer<D>(newState: TableState<D>, action: ActionType, previousState: TableState<D>, instance: TableInstance<D> | undefined): TableState<D> {
                    return undefined;
                },
                useControlledState<D>(state: TableState<D>, meta: Meta<D>): TableState<D> {
                    return undefined;
                }
            },
                useFilters,
                useGroupBy,
                useSortBy,
                useExpanded,
                usePagination,
                useRowSelect );

shamilsun avatar Nov 19 '20 06:11 shamilsun

@tannerlinsley please reopen this issue, sure i cant find working example for classed component.

shamilsun avatar Nov 19 '20 06:11 shamilsun

i runned with classed component, based on @ggascoigne suggestion, but this doesnt looks like tiny, better if project will be support with classed react component. i know its possible but need to write all needable methods for initiate useTables for me is difficult with time reason.

shamilsun avatar Nov 19 '20 11:11 shamilsun