Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple rendering useReactTable hook with nextjs14 #5570

Closed
2 tasks done
d1d4r opened this issue May 19, 2024 · 1 comment
Closed
2 tasks done

multiple rendering useReactTable hook with nextjs14 #5570

d1d4r opened this issue May 19, 2024 · 1 comment

Comments

@d1d4r
Copy link

d1d4r commented May 19, 2024

TanStack Table version

8.16.0

Framework/Library version

^18

Describe the bug and the steps to reproduce it

hi there

i fetched data from TransactionTabel in nextjs 14 usring defualt rendreing SSR

// TransactionTabel.jsx component

import { columns } from "@/app/transactions/_component/columns";
import React from "react";
import { getTransactions } from "../data/transactionData";
import { RootTabel } from "./tabel/RootTabel";

export default async function TransactionTabel({page}) {

  const { transactions, error } = await getTransactions(page);

  if (!transactions) {
    return <p>{error}</p>;
  }


  return (
    <RootTabel
      columns={columns}
      data={transactions}
      page={page}
    />
  );
}

then passed data to RootTabel component

//RootTabel.jsx component


"use client";

import {
  flexRender,
  getCoreRowModel,
  useReactTable,
} from "@tanstack/react-table";

import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import Link from "next/link";

export function RootTabel({ columns, data, page }) {
  const table = useReactTable({
    data,
    columns,
    getCoreRowModel: getCoreRowModel(),
  });

  console.log("rendered");

  return (
    <>
      <div className="border rounded-md">
        <Table>
          <TableHeader>
            {table.getHeaderGroups().map((headerGroup) => (
              <TableRow key={headerGroup.id}>
                {headerGroup.headers.map((header) => {
                  return (
                    <TableHead key={header.id}>
                      {header.isPlaceholder
                        ? null
                        : flexRender(
                            header.column.columnDef.header,
                            header.getContext()
                          )}
                    </TableHead>
                  );
                })}
              </TableRow>
            ))}
          </TableHeader>
          <TableBody>
            {table.getRowModel().rows?.length ? (
              table.getRowModel().rows.map((row) => (
                <TableRow
                  key={row.id}
                  data-state={row.getIsSelected() && "selected"}
                >
                  {row.getVisibleCells().map((cell) => (
                    <TableCell key={cell.id}>
                      {flexRender(
                        cell.column.columnDef.cell,
                        cell.getContext()
                      )}
                    </TableCell>
                  ))}
                </TableRow>
              ))
            ) : (
              <TableRow>
                <TableCell
                  colSpan={columns.length}
                  className="h-24 text-center"
                >
                  No results.
                </TableCell>
              </TableRow>
            )}
          </TableBody>
        </Table>
      </div>
      <div className="flex items-center justify-end py-4 space-x-2">
        <Link
          href={`transactions?page=${+page - 1}`}
          variant="outline"
          size="sm"
          onClick={() => table.previousPage()}
          disabled={!table.getCanPreviousPage()}
        >
          Previous
        </Link>
        <Link
          href={`transactions?page=${+page + 1}`}
          variant="outline"
          size="sm"
          onClick={() => table.nextPage()}
          disabled={!table.getCanNextPage()}
        >
          Next
        </Link>
      </div>
    </>
  );
}

but this component render multiple time some time 2, some time 3

why this happen

sandbox
app/transactions/_component/tabel/RootTabel.jsx
app/transactions//RootTabel.jsx

how i can fix it thank you for explaining and helping

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

https://codesandbox.io/p/github/d1d4r/expense-tracker/master?file=%2Fsrc%2Fapp%2Ftransactions%2F_component%2Ftabel%2FRootTabel.jsx&workspaceId=9d63e763-176b-4cfc-b16d-ba5abfabb303

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

None

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
@KevinVandy
Copy link
Member

I'm not able to view the sandbox, but multiple re-renders (especially with React Strict Mode) is expected. Without seeing all of your code, it could be that parent components are causing extra renders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants