๊ฐ๋ง์ ๋ค์ ๋ค์ด์จ ๋์ฒ ํ๋ก์ ํธ๋ ์ค๋ฅ ํฌ์ฑ์ด์๋ค ^_^
ํ๋ฃจ ์ข ์ผ ์ฌ๊ธฐ๋ฅผ ๋ง์ผ๋ฉด ์ ๊ธฐ์ ์์ด๋์ค๋ ์ค๋ฅ๋ฅผ ์ก์๊ณ ๋ณธ๋ก ์ ์ด์ ์์ผ ์์!
TanStack Table | React Table, Solid Table, Svelte Table, Vue Table
Headless UI for building powerful tables & datagrids with TS/JS, React, Solid, Svelte and Vue
tanstack.com
AG-grid๋ฅผ ์ธ๊น, tanstack table์ ์ธ๊น ๊ณ ๋ฏผํ๋ค๊ฐ ๊ฐ์ฅ ์ต๊ทผ์ ์ผ๋ tanstacktable์ด๋ ํ์คํ ์๊ณ ๊ฐ์ ํ๊ณ ๊ฒฐ์ ํ๋ค
$ npm install @tanstack/react-table
์ผ๋จ ์ค๋์ ๋ชฉํ๋, ์ ๋ฒ์ ์ค๊ตฌ๋๋ฐฉ์ผ๋ก ๋ถ๋ฌ์๋ ๋๋ฌผ์ ์ฒ ์ฃผ๋ฏผ ์ ๋ณด๋ค์ ํ ์ด๋ธ์ ๋ฃ์ด ์ถ๋ ฅํ๋ ๊ฒ..
๊ทธ๋ฌ๋ ค๋ฉด ์ผ๋จ 1245๋ ์ ์ฏค ๋ถ๋ฌ์๋ ๋ฐ์ดํฐ๊ฐ ์ด๋ค ์์ธ์ง ํ์ธํด ๋ด์ผํ๋ค. (ใ _ใ )
์ด ์ค์์ ์ผ๋จ ๋์ถฉ ๋ช ๊ฐ๋ง ์ถ๋ ค์ ์ปฌ๋ผ์ผ๋ก ์งฐ๋ค.
ColumnList.ts
import { createColumnHelper } from "@tanstack/react-table";
const columnHelper = createColumnHelper<any>();
export const VillagersColumns = [
columnHelper.accessor("name", { header: "์ด๋ฆ", size: 60 }),
columnHelper.accessor("gender", { header: "์ฑ๋ณ", size: 60 }),
columnHelper.accessor("species", { header: "์ข
", size: 100 }),
];
<any>๋ฅผ ์ ์ด์ฃผ์ง ์์ผ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๊ฐ ๋์จ๋ค.
๋ <any>๋ฅผ ์ ์๊ตฐ.. ํ๋ฉด์ ์ง๊ธ ํ์ ์ ์์ ํ๊ณ ์๋ค.
์์ ํ ์ฝ๋
import { createColumnHelper } from "@tanstack/react-table";
interface Villager {
name?: string;
gender?: string;
species?: string;
}
const columnHelper = createColumnHelper<Villager>();
export const VillagersColumns = [
columnHelper.accessor("name", { header: "์ด๋ฆ", size: 60 }),
columnHelper.accessor("gender", { header: "์ฑ๋ณ", size: 60 }),
columnHelper.accessor("species", { header: "์ข
", size: 100 }),
];
๊ทธ๋ฆฌ๊ณ ์ด์ ์ํ๋ ํ์ด์ง์ ๋ถ๋ฌ์ค๋ฉด ๋๋ค.
villager/page.tsx
import VillagerList from "./VillagerList";
export default function VillagersPage() {
return (
<>
<VillagerList />;
</>
);
}
VillagerList.tsx
"use client"
...
import {
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";
import { VillagersColumns } from "@/constants/ColumnList";
...
// ์ฃผ๋ฏผ ์ ๋ณด๋ uniqueData์ ๋ด๊น (์ด์ ๊ฒ์๊ธ)
const table = useReactTable({
data:uniqueData,
columns:VillagersColumns,
getCoreRowModel: getCoreRowModel(),
});
return (
<>
<table>
<thead>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<th key={header.id} style={{ width: header.getSize() }}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</th>
))}
</tr>
))}
</thead>
<tbody>
{table.getRowModel().rows.map((row) => (
<tr key={row.id}>
{row.getVisibleCells().map((cell) => (
<td key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
</>
);
์ด๋ ๊ฒ ์งํํ๋๋ ๋ฌดํ์ ํธ์ถ์ด ๋๋ ์ค๋ฅ๊ฐ ๋ฌ๋ค
Error: Rendered more hooks than during the previous render.
๊ทธ๋์ ํ ์ด๋ธ์์ฒด๋ฅผ ๋ค๋ฅธ component๋ก ๋นผ์ client๋ก ์ฌ์ฉ๋์ง ์๊ฒ ํ๋ค.
์ด์ฐจํผ ๊ณต์ฉ์ผ๋ก ์ธ ์ปดํฌ๋ํธ์ฌ์, ํ ์คํธ๋ง ํด๋ณด๊ณ ๋ฐ๋ก ๋ง๋ค์ด๋๋ คํ๋๋ฐ ์ด๋ ๊ฒ ๋๊ฑฐ ๋ฐ๋ก ์งํํจ
TanstackTable.tsx
import {
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";
export default function TanstackTable({
data,
columns,
}: {
data: any[];
columns: any[];
}) {
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
});
return (
<table>
<thead>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<th key={header.id} style={{ width: header.getSize() }}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</th>
))}
</tr>
))}
</thead>
<tbody>
{table.getRowModel().rows.map((row) => (
<tr key={row.id}>
{row.getVisibleCells().map((cell) => (
<td key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
);
}โ
๋๋ฒ์งธ ์ค๋ฅ๋ ๋๋ฌด๋๋ ์์ฃผ๋ดค์ง๋ง ํ๋ฒ์ ์ถ๊ฐํ ์๊ฐ์ ํ์ง ์๋ map์ค๋ฅ..
TypeError: Cannot read properties of undefined (reading 'map')
table๋ถ๋ถ์ ?๋ฅผ ๋ฃ์ด๋ ์ค๋ฅ ํด๊ฒฐ์ด ๋์ง ์์์,
if (!data || !columns) {
return <div>No data or columns provided.</div>;
}
๋ฅผ ์ถ๊ฐํด์ฃผ์๋๋ ํด๊ฒฐ์ด ๋์๋ค.
๊ทธ๋ฆฌ๊ณ ์ด์ ์๊น ํ์ด์ง์์ data์ columns์ ์ ๋ณด๋ฅผ ๋๊ฒจ์ค๋ค.
VillagersList.tsx
<TanstackTable data={uniqueData} columns={VillagersColumns} />
๋ค๋ฌ์ด์ผํ ๊ฒ์ด ์ฐ๋๋ฏธ์ง๋ง, ์ผ๋จ์ ๋ถ๋ฌ์ค๋ ๋ฐ์ ์ฑ๊ณตํ๋ค.