You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React报错:Rendered fewer hooks than expected问题排查求助

React Hooks错误:Rendered fewer hooks than expected 原因分析与解决

原因分析

问题核心在于Hook调用的一致性被破坏,具体触发点在getActionComponent的使用逻辑:

  1. getActionComponent()返回的按钮组件内部使用了useNavigate Hook,但该函数在ClientsPage的渲染阶段直接执行,其结果作为actionComponent.getActionComponent传递给Table组件。
  2. 当clientsInLocalUpdated为空数组/未定义时,Table组件的values.map不会执行,包含useNavigate的按钮组件不会被渲染,此时无该Hook调用;当clientsInLocalUpdated有数据时,按钮组件被渲染,useNavigate被触发,导致前后渲染周期中Hook调用数量不一致,触发报错。
  3. 场景匹配:首次加载时clients未就绪(为空),数据加载完成后clientsInLocalUpdated更新为有数据数组,Hook数量从0变为1;输入空值搜索时,clientsInLocalUpdated切换回完整clients数组,同样触发Hook数量变化。

解决建议

1. 修正getActionComponent的定义与使用

不要在渲染阶段直接执行函数返回组件,确保Hook始终在组件顶层调用:

// ClientsPage.tsx 中替换原getActionComponent定义
const ActionButton = (props: any) => {
    const navigate = useNavigate();
    return <Button
        size="small"
        onClick={() => navigate(`/clients/${props.row.id}`)}
        {...props}
        color="secondary"
    >
        Create Punchlist
    </Button>
};

// 传递组件渲染函数给Table
<Table
    heads={HEAD_CLIENT_TABLE}
    values={clientsInLocalUpdated}
    styles={STYLES_BY_COLUMN}
    hasActions
    actionComponent={{ getActionComponent: (props) => <ActionButton {...props} />, title: "Actions" }}
/>

2. 优化clientsInLocalUpdated状态管理

  • 初始状态设为空数组,避免clients初始为undefined引发的问题:
const [clientsInLocalUpdated, setClientsInLocalUpdated] = useState<IDataRow[]>([]);
  • 调整依赖项,确保clients更新时同步状态:
useEffect(() => {
    setClientsInLocalUpdated(clients || []);
}, [clients]); // 依赖改为clients,而非isLoading

3. 精简onFilterClients的依赖与逻辑

移除不必要的依赖,避免无效重渲染:

const onFilterClients = useCallback((searchInput: string) => {
    if (searchInput === undefined) return;

    if (searchInput === '') {
        setClientsInLocalUpdated(clients || []);
    } else {
        const filteredClients = (clients || []).filter(
            (item) =>
                item && item?.name && item.name.toLowerCase().includes(searchInput.toLowerCase())
        );
        setClientsInLocalUpdated(filteredClients);
    }
}, [clients]); // 仅依赖clients

内容的提问来源于stack exchange,提问作者Freddy Valverde Gallardo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 19:53:08