From 0ff8d38aa2b727e65dda07f09f6509a2fad4a4e6 Mon Sep 17 00:00:00 2001 From: nabettu Date: Mon, 19 Oct 2026 15:32:35 +0900 Subject: [PATCH] fix(mobile): custom header views no longer crash with "Couldn't find a navigation context" Header option signatures walked into React elements, reaching dev-only internals such as the owner fiber and React Navigation's context defaults, whose getters throw. Sign elements by type, key, and props instead. Co-Authored-By: Claude Opus 5.5 --- .../src/native/NativeHeaderToolbar.test.tsx | 28 ++++++++++++++++++- apps/mobile/src/native/StackHeader.tsx | 9 +++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/apps/mobile/src/native/NativeHeaderToolbar.test.tsx b/apps/mobile/src/native/NativeHeaderToolbar.test.tsx index a811d7f797a22..4ad4c210ea152 100644 --- a/apps/mobile/src/native/NativeHeaderToolbar.test.tsx +++ b/apps/mobile/src/native/NativeHeaderToolbar.test.tsx @@ -1,6 +1,6 @@ // @vitest-environment jsdom import type { NativeStackHeaderItem } from "@react-navigation/native-stack"; -import { act } from "react"; +import { act, createContext, use } from "react"; import { createRoot, type Root } from "react-dom/client"; import { afterEach, expect, it, vi } from "vite-plus/test"; @@ -99,3 +99,29 @@ it.each(["left", "right", "bottom"] as const)( expect(harness.openedDocuments).toEqual(["first/guide.pdf"]); }, ); + +// React Navigation's context defaults throw from getters, and dev elements keep their owner fiber. +const ThrowingDefaultContext = createContext({ + get value(): never { + throw new Error("Couldn't find a navigation context."); + }, +}); + +function CustomViewToolbar() { + use(ThrowingDefaultContext); + return ( + + + Status + + + ); +} + +it("applies custom header views created under contexts whose defaults throw", () => { + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + root ??= createRoot(document.createElement("div")); + act(() => root!.render()); + + expect(harness.renderedItems.map((item) => item.type)).toEqual(["custom"]); +}); diff --git a/apps/mobile/src/native/StackHeader.tsx b/apps/mobile/src/native/StackHeader.tsx index 3b1fb0be06fa7..af1a46b1a11f5 100644 --- a/apps/mobile/src/native/StackHeader.tsx +++ b/apps/mobile/src/native/StackHeader.tsx @@ -3,7 +3,7 @@ import type { NativeStackNavigationOptions, NativeStackNavigationProp, } from "@react-navigation/native-stack"; -import { use, useEffect, useLayoutEffect, useMemo, useRef } from "react"; +import { isValidElement, use, useEffect, useLayoutEffect, useMemo, useRef } from "react"; import { NativeWorkspaceTabActivityContext } from "./v5-workspace-context"; import type { ColorValue } from "react-native"; @@ -81,6 +81,13 @@ function optionsSignature(value: unknown, seen = new WeakSet()): string // React refs carry mutable native instances that must not make static // screen options appear different after every render. if ("current" in object) return "[ref]"; + // Custom header views are elements. Only their type, key, and props describe them; + // dev-only internals such as the owner fiber reach context defaults that throw. + if (isValidElement(object)) { + const type = object.type as { displayName?: string; name?: string } | string; + const typeName = typeof type === "string" ? type : (type.displayName ?? type.name); + return `element:${typeName}:${String(object.key)}:${optionsSignature(object.props, seen)}`; + } return `{${Object.keys(value as Record) .sort() .map(