in react table all the data its showing but when i want to click the header for sorting its throws errors “Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.” How to resolve that one
import React, { useMemo } from "react"; import { useTable, useSortBy } from "react-table"; import pack from "./pack.json"; export default function App() { const COLUMN = [ { Header: "ID", accessor: "id" }, { Header: "Name", accessor: "name" }, { Header: "Age", accessor: "age" } ]; const columns = useMemo(() => { COLUMN, []; }); const data = useMemo(() => { pack, []; }); const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable( { columns: COLUMN, data: pack }, useSortBy ); return ( <> <table {...getTableProps()}> <thead> {headerGroups.map((headerGroup) => ( <tr {...headerGroup.getFooterGroupProps()}> {headerGroup.headers.map((column) => ( <th {...column.getHeaderProps(column.getSortByToggleProps())}> {column.render("Header")} <span> {column.isSorted ? (column.isSortedDesc ? ">" : "<") : ""} </span> </th> ))} </tr> ))} </thead> <tbody {...getTableBodyProps()}> {rows.map((row) => { prepareRow(row); return ( <tr {...row.getRowProps()}> {row.cells.map((cell) => { return ( <td {...cell.getCellProps()}>{cell.render("Cell")}</td> ); })} </tr> ); })} </tbody> </table> ; </> ); }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This is happening because you are not returning COLUMN and pack from the useMemo, as they are wrapped in curly braces.
Also, instead of providing the memoized version of columns and data to useTable you’re passing in the original variables.
You could instead do something like this:
import React, { useMemo } from "react"; import { useTable, useSortBy } from "react-table"; import pack from "./pack.json"; export default function App() { const columns = useMemo( () => [ { Header: "ID", accessor: "id" }, { Header: "Name", accessor: "name" }, { Header: "Age", accessor: "age" } ], [] ); const data = useMemo(() => pack, []); const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable( { columns, data }, useSortBy ); return ( <> <table {...getTableProps()}> <thead> {headerGroups.map((headerGroup) => ( <tr {...headerGroup.getFooterGroupProps()}> {headerGroup.headers.map((column) => ( <th {...column.getHeaderProps(column.getSortByToggleProps())}> {column.render("Header")} <span> {column.isSorted ? (column.isSortedDesc ? ">" : "<") : ""} </span> </th> ))} </tr> ))} </thead> <tbody {...getTableBodyProps()}> {rows.map((row) => { prepareRow(row); return ( <tr {...row.getRowProps()}> {row.cells.map((cell) => { return ( <td {...cell.getCellProps()}>{cell.render("Cell")}</td> ); })} </tr> ); })} </tbody> </table> ; </> ); }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0