Replies: 21 comments 27 replies
-
Yeah, not really good first experience. I hope it's going to be fixed as soon as possible. Looks like it's created for an old version of the library. |
Beta Was this translation helpful? Give feedback.
-
@HadiSDev Can you post the version of |
Beta Was this translation helpful? Give feedback.
-
I'm not the original poster, but I also got a (different) Typescript type error for the quick-start guide.
As for my dependencies, I'm using: "react-table": "^7.5.0",
"@types/react-table": "^7.0.23", |
Beta Was this translation helpful? Give feedback.
-
I'm getting the same error. Did anyone find a solution? A temporary hack is to just add |
Beta Was this translation helpful? Give feedback.
-
What worked for me:
This is what I've used to get rid of this error. I don't really know if this is the best approach but it worked for me. P.S.: Ran into this error today and it took a bit of time until I found out what to do in this situation. |
Beta Was this translation helpful? Give feedback.
-
Apparently the solution was in the documentation the whole time. I just never looked into it until later because it comes after the quickstart example is finished in the documentation. You can find the types for all props here: https://react-table.tanstack.com/docs/api/useTable If you're looking for an example, I made a TypeScript version of the quickstart example here: TLDR: data: Array<any>;
columns: Array<Column>; |
Beta Was this translation helpful? Give feedback.
-
Hi everyone, I was facing the same issues with typescript and react-table, with so little examples it wasn't obvious. Here's my solution (create your own Table component, typescript should detect types errors): Table.tsx import React from "react";
import { UseTableInstanceProps } from "react-table";
export interface TableNewProps<T extends object>
extends Pick<
UseTableInstanceProps<T>,
| "getTableProps"
| "headerGroups"
| "getTableBodyProps"
| "prepareRow"
| "rows"
> {}
export function Table<T extends object>(props: TableNewProps<T>) {
const {
getTableProps,
headerGroups,
getTableBodyProps,
rows,
prepareRow
} = props;
return (
<table {...getTableProps()} style={{ border: "solid 1px blue" }}>
<thead>
{headerGroups.map((headerGroup) => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => (
<th
{...column.getHeaderProps()}
style={{
borderBottom: "solid 3px red",
background: "aliceblue",
color: "black",
fontWeight: "bold"
}}
>
{column.render("Header")}
</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row) => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map((cell) => {
return (
<td
{...cell.getCellProps()}
style={{
padding: "10px",
border: "solid 1px gray",
background: "papayawhip"
}}
>
{cell.render("Cell")}
</td>
);
})}
</tr>
);
})}
</tbody>
</table>
);
} App.tsx import React from "react";
import { useTable, Column } from "react-table";
import { Table } from "./Table";
import "./styles.css";
type Data = {
id: string;
date: string;
firstName: string;
lastName: string;
email: string;
view: React.ReactNode;
};
const createArr = (n: number): Data[] => {
const data: Data[] = [];
for (let i = 0; i < n; i += 1) {
data.push({
id: `ID-${Math.random().toFixed(4)}`,
date: new Date().toDateString(),
firstName: `Rick #${i}`,
lastName: `Sanchez #${i}`,
email: `morty_${i}@rick.space`,
view: <button>View</button>
});
}
return data;
};
export default function App() {
const data = React.useMemo<Data[]>(() => createArr(10), []);
const columns = React.useMemo<Column<Data>[]>(
() => [
{
Header: "ID",
accessor: "id"
},
{
Header: "Date Requested",
accessor: "date"
},
{
Header: "First Name",
accessor: "firstName"
},
{
Header: "Last Name",
accessor: "lastName"
},
{
Header: "Email",
accessor: "email"
},
{
accessor: "view"
}
],
[]
);
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow
} = useTable<Data>({ columns, data });
return (
<div className="App">
<Table<Data>
getTableProps={getTableProps}
getTableBodyProps={getTableBodyProps}
headerGroups={headerGroups}
rows={rows}
prepareRow={prepareRow}
/>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
-
Hi, any updates ? |
Beta Was this translation helpful? Give feedback.
-
Hoping to use this lib because I found it really good but it broke my interfaces to a point that I can't keep using it. |
Beta Was this translation helpful? Give feedback.
-
Casting an Here's my example code: Table.tsx
ComponentWIthTable.tsx
The error
|
Beta Was this translation helpful? Give feedback.
-
Here's the fix! ggascoigne/react-table-example#3 (comment)
|
Beta Was this translation helpful? Give feedback.
-
I had the same problem and found another solution. you have to define the accessor strings as const like this:
|
Beta Was this translation helpful? Give feedback.
-
I'll add that I try and keep https://github.com/ggascoigne/react-table-example up to date, so it should be a reasonable example. I accept that a simpler example might also be useful. |
Beta Was this translation helpful? Give feedback.
-
I fixed it by adding a type to my useMemo:
|
Beta Was this translation helpful? Give feedback.
-
Hey guys, I think that I found a solution! I created a simple interface that I called
Then, I used the interface in the first
And no more error :) Here you can find the complete code I've used:
|
Beta Was this translation helpful? Give feedback.
-
I love typescript. It helps you with typings and autocompletes, so you won't spend much time. Also, it lets you spend a year fixing the basic usage of a table with a bunch of strangers. Wait... |
Beta Was this translation helpful? Give feedback.
-
Fixed Such a library doesn't have a TS guide! Sad! Anyway, I had this fix folks just by adding the
|
Beta Was this translation helpful? Give feedback.
-
For anyone finding this answer in 2023, it looks like there is a new table (v8) which worked fairly well out of the box: |
Beta Was this translation helpful? Give feedback.
-
Hi there! It looks like the error is related to a type mismatch in your code. Specifically, the type To fix the error, you should ensure that the objects in the Here's an example of how you could define the
Note: Main point is |
Beta Was this translation helpful? Give feedback.
-
Same error, quick fix, set it to const to make it read only.
|
Beta Was this translation helpful? Give feedback.
-
Works for me.
interface ITable {
name: string;
age: number;
}
const colHelper = createColumnHelper<ITable>();
const columns = [
colHelper.accessor('name', {
header: () => {...},
cell: (info) => <span>{info.getValue()}</span>
}),
colHelper.accessor('age', {
header: () => {...},
cell: (info) => <span>{info.getValue()}</span>
})
// and here define our type for columns
] as Column<ITable>[]; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I just started using react table and I keep getting a type error when trying to use the quick start example.
Beta Was this translation helpful? Give feedback.
All reactions