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

NextJS+Redux Toolkit渲染时更新组件警告问题求助

问题求助:NextJS + Redux Toolkit 组件渲染时更新状态警告

我是NextJS新手,遇到了"Cannot update a component while rendering a different component"的警告,和同类问题场景一致,但我的问题出在Redux Toolkit使用中。警告指向以下两行useAppSelector代码:

  • const requestNumber = useAppSelector((state) => state.createRequest.requestNumber) ?? '';
  • const userInfo: User | undefined = useAppSelector( (state) => state.auth.userInfo );

以下是我的CreateLayout组件代码:

const CreateLayout = ({ children }: { children: React.ReactNode }) => {
  const { Content } = Layout;
  // const selectedBrandName = useAppSelector((state) => state.createRequest.name);
  const router = useRouter();
  const dispatch = useAppDispatch();
  const spinOn = useAppSelector((state) => state.createRequest.spinOn);
  const requestNumber =
    useAppSelector((state) => state.createRequest.requestNumber) ?? '';
  const userInfo: User | undefined = useAppSelector(
    (state) => state.auth.userInfo
  );

  const response = useGetCreateRequestInfo({
    params: {
      // requestNumber: requestNumber,
      requestNumber: '#130',
      tenant: process.env.NEXT_PUBLIC_DOMAIN ?? '',
      userId: userInfo?.id ?? '',
    },
    options: {
      refreshInterval: 10000,
    },
  });
  if (response.data?.data) {
    dispatch(setCreateRequestData(response.data.data));
  }
  return (
    <Layout className="bg-white">
      <section className="flex flex-row gap-[12px] border-b-[#E4E8ED] border-[1px] p-[16px]">
        <LeftOutlined
          onClick={() => {
            dispatch(setDataImageOtherUpload({}));
            dispatch(setDataImageUpload({}));
            router.back();
          }}
        />
        <div className="text-[16px] leading-[24px] font-[500] flex flex-col">
          <div>Create New Request</div>
          <div className="text-[12px] leading-[16px] font-[400] truncate w-[120px]">
            {response.data?.data?.insuranceDetail.note}
          </div>
        </div>
      </section>
      <Content>{children}</Content>
      {spinOn && (
        <Spin className="absolute w-full h-screen flex items-center justify-center backdrop-blur-sm bg-white/30" />
      )}
    </Layout>
  );
};

export default CreateLayout;

希望得到各位的帮助,感谢阅读!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 02:05:30