React JS:未捕获的错误:超过最大更新深度

huangapple go评论100阅读模式
英文:

React JS : Uncaught Error: Maximum update depth exceeded

问题

我正在尝试使用React-table在表格中显示数据,但页面是空白的,数据没有渲染出来。我已经仔细检查了代码,但似乎找不到问题所在。有人能否请审查我的代码,并帮助我找出可能出错的地方?

问题似乎与我如何使用react-table以及Redux来获取和显示数据有关。我怀疑列(columns)和数据(data)变量可能存在问题,或者可能与我如何使用react-table中的useTable有关。

任何帮助将不胜感激。谢谢!

英文:

I'm trying to display data on a table using React-table, but the page is blank, and the data is not rendering. I have checked the code thoroughly, but I can't seem to find the issue. Can someone please review my code and help me identify where it might be going wrong?

  1. import React, { useState, useEffect, useMemo } from 'react';
  2. import { useTable } from 'react-table';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
  5. import { COLUMNS } from './Column';
  6. import NavigationDashbooard from './UsersNavigationBigScreen';
  7. import Button from '../components/Button';
  8. import { loading } from '../assets';
  9. import { fetchingAllUsers } from '../states/features/users/usersSlice';
  10. import NavigationDashboardSmall from './UsersNavigationSmallScreen';
  11. const AdminManageUserContainer = () => {
  12. const dispatch = useDispatch();
  13. const { isPending, totalpages, userList, networkError } = useSelector(
  14. (state) => {
  15. return state.users;
  16. }
  17. );
  18. const columns = useMemo(() => {
  19. return (COLUMNS, [])
  20. });
  21. const data = useMemo(() => {
  22. return (userList, [])
  23. });
  24. const tableInstance = useTable({ columns: columns, data: data });
  25. const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
  26. tableInstance;
  27. useEffect(() => {
  28. dispatch(fetchingAllUsers(1));
  29. }, []);
  30. return (
  31. <div className='manage_accounts_wrapper h-screen'>
  32. <NavigationDashbooard />
  33. <div className='manage_accounts_content'>
  34. <h1 className='bigTitle_users mt-10'>Manage Users</h1>
  35. <div className='createUserBox mb-10'>
  36. <h1 className='listOfUsers '>List of users</h1>
  37. <Button
  38. className='primary-btn create_user_btn'
  39. value='+ Add user'
  40. route='/'
  41. onClick={() => {}}
  42. />
  43. <Button
  44. className='create_user_btn_small'
  45. value='+'
  46. route='/'
  47. onClick={() => {}}
  48. />
  49. </div>
  50. <div className='manage_accounts_content_table h-4.5/6'>
  51. <table {...getTableProps()}>
  52. <thead>
  53. {headerGroups.map((headerGroup) => {
  54. return (
  55. <tr {...headerGroup.getHeaderGroupProps()}>
  56. {headerGroup.headers.map((column) => {
  57. return (
  58. <th {...column.getHeaderProps()}>
  59. {column.render('Header')}
  60. </th>
  61. );
  62. })}
  63. </tr>
  64. );
  65. })}
  66. </thead>
  67. <tbody {...getTableBodyProps()}>
  68. {rows.map((row) => {
  69. prepareRow(row);
  70. return (
  71. <tr {...row.getRowProps()}>
  72. {row.cells.map((cell ) => {
  73. return (
  74. <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
  75. );
  76. })}
  77. </tr>
  78. );
  79. })}
  80. </tbody>
  81. </table>
  82. {isPending && (
  83. <div className='loading_div w-full'>
  84. Loading...
  85. <img src={loading} alt='' />
  86. </div>
  87. )}
  88. {networkError && (
  89. <div className='loading_div w-full'>
  90. Server or Network Error{' '}
  91. <FontAwesomeIcon icon='fa-solid fa-exclamation-circle' />
  92. </div>
  93. )}
  94. </div>
  95. </div>
  96. <NavigationDashboardSmall />
  97. </div>
  98. );
  99. };
  100. export default AdminManageUserContainer;

The problem seems to be related to how I am using react-table along with Redux to fetch and display data. I suspect there might be an issue with the columns and data variables, or possibly with the way I am using useTable from react-table.

Any help would be greatly appreciated. Thank you!

答案1

得分: 0

你在使用useMemo时没有正确获取列或数据。使用逗号分隔符表示:计算由逗号分隔的所有表达式并返回最后一个

  1. return (COLUMNS, [])
  2. // 这将返回 []

你应该以正确的方式使用useMemo。第一个参数是一个返回值的函数,第二个是依赖数组。

  1. const columns = useMemo(() => {
  2. return COLUMNS;
  3. }, []);
  4. // 返回dataList的简短方式
  5. const data = useMemo(() => dataList, []);
英文:

You are not getting the columns or the data in the way you use your useMemo.
Using a comma separator stands for: valuate all the expressions separated by commas and return the last one.

  1. return (COLUMNS, [])
  2. // this returns []

You should use useMemo in the correct way. First argument is a function which return a value, second one is the dependency array.

  1. const columns = useMemo(() => {
  2. return COLUMNS;
  3. }, []);
  4. // short way of returning dataList
  5. const data = useMemo(() => dataList, []);

huangapple
  • 本文由 发表于 2023年7月17日 23:55:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定