You need to enable JavaScript to run this app. Material UI Dialog中使用React-table选行触发最大更新深度超限错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Material UI Dialog中使用React-table选行触发最大更新深度超限错误如何解决

问题根源
  • 父组件AddCodesToCaseDialog给MMTable传递的exportSelectedRows是内联匿名函数,每次父组件调用setSelectedCodes更新状态时,这个函数都会生成全新的引用,导致MMTable组件不必要重渲染。
  • react-table返回的selectedFlatRows默认没有做引用稳定化处理,每次MMTable重渲染都会生成新的数组实例,触发监听它的useMountedLayoutEffect执行。
  • effect执行后再次调用exportSelectedRows触发setSelectedCodes,父组件再次更新,形成无限重渲染循环。
可行修复方案

方案1:缓存exportSelectedRows函数引用

用useCallback包裹导出选中行的处理函数,保证引用稳定,避免不必要的子组件重渲染,修改父组件代码如下:

// 导入useCallback
import React, { useState, useMemo, useCallback } from 'react';

function AddCodesToCaseDialog(props) {
  const [selectedCodes, setSelectedCodes] = React.useState("test");

  // 新增:用useCallback包裹处理函数,依赖项为空数组保证引用稳定
  const handleExportSelectedRows = useCallback((selectedRows) => {
    setSelectedCodes(selectedRows.map((entry, index) => entry.values))
  }, [])

  const columns = React.useMemo(
    () => [
      {
        Header: 'Code',
        accessor: 'code',
      },
      {
        Header: 'Beschreibung',
        accessor: 'description',
      },
    ],
    []
  )
  const data = [
    { code: "R111", description: "asdasasgsgeqea" },
    { code: "R12131", description: "YYYYYYYYYYa" },
    { code: "as111", description: "XXXXXXXXXXXXXXa" },
    { code: "CC111", description: "BBBBBBfa" },
    { code: "R1sss2131", description: "YYYYYYyycYYYYa" },
    { code: "asddd111", description: "XXXXXyyyXXXXXXXXXa" },
    { code: "CCggg111", description: "BBBgggBBBfa" },
  ]

  const handleCloseModal = () => {
    props.setOpen(false);
  };

  const dialogContent = (
    <Container>
      <div style={{
        maxWidth: "100%",
        width: "1000px",
        minHeight: "500px",
        flexGrow: 1,
        position: "relative",
      }}>
        <MMTable columns={columns} data={data} tableName={"Verfügbare Codes:"}
          modalView={true}
          withBorder={false}
          // 替换内联函数为缓存后的函数
          exportSelectedRows={handleExportSelectedRows}
        />
      </div>
      <pre>{JSON.stringify(selectedCodes, null, 2)}</pre>
    </Container>
  )

  const dialogActions = [
    <StyledButton onClick={(event) => {}}>Abbrechen</StyledButton>,
    <StyledButton onClick={(event) => {}}>Auswahl hinzufügen</StyledButton>
  ]

  return (
    <ResponsiveDialogSkeleton openModal={props.open}
      handleCloseModal={handleCloseModal}
      dialogTitle={"Codes hinzufügen"}
      dialogContent={dialogContent}
      dialogActions={dialogActions}
    />
  );
}
export default AddCodesToCaseDialog;

该方案改动最小,可解决90%以上同类型场景问题。

方案2:优化selectedFlatRows的依赖判断逻辑

如果方案1没有完全解决,可修改MMTable中的effect逻辑,对比selectedFlatRows的实际内容而不是引用判断是否需要执行:

// 可以引入lodash的isEqual做深比较,或者自己实现适配业务的浅比较逻辑
import { isEqual } from 'lodash';

useMountedLayoutEffect(() => {
  exportSelectedRows(selectedFlatRows)
// 用内容比较代替默认的引用比较
}, [isEqual(selectedFlatRows)]);

// 如果选中行有唯一标识id,也可以用拼接id字符串的方式简化比较
// }, [selectedFlatRows.map(row => row.id).join(',')]);

方案3:调整逻辑避免effect触发状态更新

如果业务场景允许,可以直接在选中行的点击事件回调中处理导出逻辑,代替用effect监听selectedFlatRows变化触发更新的逻辑,从根源上避免循环触发的可能。

内容的提问来源于stack exchange,提问作者Jannik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 19:06:03