Next.js Rendered more hooks than during the previous render when notFound is used in conjunction with react.Context and Suspense

I’m using the latest version of next.js (14.2.7) as of today. This is the minimal example that reproduces the error. What’s wrong with it and how can the error be avoided?

The sample loads data from two server actions. The successful server action result is provided by a react context; The failed server action calls notFound(). The not found page has a button then navigates the user to the home page. Once the user clicks on the button, the error will occur. It doesn’t really matter what the href is in the Link.

The main page consists of a provider, which contains a react suspense component. The suspense contains an async component, which calls the failing server action.

page.tsx

import React from "react";
import { willFail } from "./actions";
import { DataProvider } from "./provider";

export default async function Page() {
  return (
    <DataProvider>
      <React.Suspense>
        <FailData />
      </React.Suspense>
    </DataProvider>
  );
}

async function FailData() {
  return <div>{await willFail()}</div>;
}

The DataProvider component fetches the data in a react effect in which a server action is called.

provider.tsx

"use client";

import React from "react";
import { willSucceed } from "./actions";

export const dataContext = React.createContext(null);

export function DataProvider({ children }: { children: React.ReactNode }) {
  const [data] = React.useState(null);
  React.useEffect(() => {
    // setData
    await willSucceed();
  }, []);
  return <dataContext.Provider value={data}>{children}</dataContext.Provider>;
}

actions.ts

"use server";

import { notFound } from "next/navigation";

export async function willSucceed() {
  return null;
}

export async function willFail(): Promise<string> /* suppress type error */ {
  notFound();
}

not-found.tsx

import Link from "next/link";

export default function NotFound() {
  return (
    <div className="flex flex-col gap-8">
      <p>404 | Not found</p>
      <Link href="/" passHref>
        Retry
      </Link>
    </div>
  );
}

Here is the errors I got:

Error: Rendered more hooks than during the previous render.
    updateWorkInProgressHook react-dom.development.js:11435
    updateMemo react-dom.development.js:12613
    useMemo react-dom.development.js:13563
    useMemo react.development.js:2536
    Router app-router.tsx:334
    renderWithHooks react-dom.development.js:11121
    updateFunctionComponent react-dom.development.js:16290
    beginWork$1 react-dom.development.js:18472
    callCallback react-dom.development.js:20565
    invokeGuardedCallbackImpl react-dom.development.js:20614
    invokeGuardedCallback react-dom.development.js:20689
    beginWork react-dom.development.js:26949
    performUnitOfWork react-dom.development.js:25748
    workLoopSync react-dom.development.js:25464
    renderRootSync react-dom.development.js:25419
    recoverFromConcurrentError react-dom.development.js:24597
    performConcurrentWorkOnRoot react-dom.development.js:24542
    workLoop scheduler.development.js:256
    flushWork scheduler.development.js:225
    performWorkUntilDeadline scheduler.development.js:534
    EventHandlerNonNull* scheduler.development.js:569
    <anonymous> scheduler.development.js:630
    NextJS 4
    <anonymous> index.js:6
    NextJS 4
    <anonymous> react-dom.development.js:27
    <anonymous> react-dom.development.js:38595
    NextJS 4
    <anonymous> index.js:37
    NextJS 4
    <anonymous> client.js:3
    NextJS 4
    <anonymous> app-index.js:15
    NextJS 4
    <anonymous> app-next-dev.ts:7
    appBootstrap app-bootstrap.ts:61
    loadScriptsInSequence app-bootstrap.ts:20
    appBootstrap app-bootstrap.ts:60
    <anonymous> app-next-dev.ts:6
    NextJS 7
lockdown-install.js:1:97687
Warning: Cannot update a component (`HotReload`) while rendering a different component (`Router`). To locate the bad setState() call inside `Router`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
Router@webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/app-router.js:207:134
ErrorBoundaryHandler@webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/error-boundary.js:113:9
ErrorBoundary@webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/error-boundary.js:160:67
AppRouter@webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/app-router.js:585:47
ServerRoot@webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/app-index.js:112:27
Root@webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/app-index.js:117:24 app-index.tsx:25:19

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật