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

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

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?

import React, { useState, useEffect, useMemo } from 'react';
import { useTable } from 'react-table';
import { useDispatch, useSelector } from 'react-redux';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { COLUMNS } from './Column';
import NavigationDashbooard from './UsersNavigationBigScreen';
import Button from '../components/Button';
import { loading } from '../assets';
import { fetchingAllUsers } from '../states/features/users/usersSlice';
import NavigationDashboardSmall from './UsersNavigationSmallScreen';

const AdminManageUserContainer = () => {

  const dispatch = useDispatch();
  const { isPending, totalpages, userList, networkError } = useSelector(
    (state) => {
      return state.users;
    }
  );
   const columns = useMemo(() => {
    return (COLUMNS, [])
  });
  const data = useMemo(() => {
    return (userList, [])
  });
  const tableInstance = useTable({ columns: columns, data: data });
  const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
    tableInstance;

  useEffect(() => {
    dispatch(fetchingAllUsers(1));
  }, []);
 
  return (
    <div className='manage_accounts_wrapper h-screen'>
      <NavigationDashbooard />
      <div className='manage_accounts_content'>
        <h1 className='bigTitle_users  mt-10'>Manage Users</h1>
        <div className='createUserBox mb-10'>
          <h1 className='listOfUsers '>List of users</h1>
          <Button
            className='primary-btn create_user_btn'
            value='+  Add user'
            route='/'
            onClick={() => {}}
          />
          <Button
            className='create_user_btn_small'
            value='+'
            route='/'
            onClick={() => {}}
          />
        </div>
        <div className='manage_accounts_content_table h-4.5/6'>
          <table {...getTableProps()}>
            <thead>
              {headerGroups.map((headerGroup) => {
                return (
                  <tr {...headerGroup.getHeaderGroupProps()}>
                    {headerGroup.headers.map((column) => {
                      return (
                        <th {...column.getHeaderProps()}>
                          {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()}>{cell.render('Cell')}</td>
                      );
                    })}
                  </tr>
                );
              })}
            </tbody>
          </table>

          {isPending && (
            <div className='loading_div w-full'>
              Loading...
              <img src={loading} alt='' />
            </div>
          )}
          {networkError && (
            <div className='loading_div w-full'>
              Server or Network Error{' '}
              <FontAwesomeIcon icon='fa-solid fa-exclamation-circle' />
            </div>
          )}
        </div>
      </div>
      <NavigationDashboardSmall />
    </div>
  );
};
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时没有正确获取列或数据。使用逗号分隔符表示:计算由逗号分隔的所有表达式并返回最后一个

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

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

const columns = useMemo(() => {
  return COLUMNS;
}, []);

// 返回dataList的简短方式
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.

return (COLUMNS, [])
// 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.

const columns = useMemo(() => {
  return COLUMNS;
}, []);

// short way of returning dataList
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:

确定