Skip to content

Commit 0ff8d38

Browse files
nabettuclaude
andcommitted
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 <[email protected]>
1 parent 1451e7c commit 0ff8d38

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎apps/mobile/src/native/NativeHeaderToolbar.test.tsx‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @vitest-environment jsdom
22
import type { NativeStackHeaderItem } from "@react-navigation/native-stack";
3-
import { act } from "react";
3+
import { act, createContext, use } from "react";
44
import { createRoot, type Root } from "react-dom/client";
55
import { afterEach, expect, it, vi } from "vite-plus/test";
66

@@ -99,3 +99,29 @@ it.each(["left", "right", "bottom"] as const)(
9999
expect(harness.openedDocuments).toEqual(["first/guide.pdf"]);
100100
},
101101
);
102+
103+
// React Navigation's context defaults throw from getters, and dev elements keep their owner fiber.
104+
const ThrowingDefaultContext = createContext({
105+
get value(): never {
106+
throw new Error("Couldn't find a navigation context.");
107+
},
108+
});
109+
110+
function CustomViewToolbar() {
111+
use(ThrowingDefaultContext);
112+
return (
113+
<NativeHeaderToolbar placement="right">
114+
<NativeHeaderToolbar.Custom>
115+
<span>Status</span>
116+
</NativeHeaderToolbar.Custom>
117+
</NativeHeaderToolbar>
118+
);
119+
}
120+
121+
it("applies custom header views created under contexts whose defaults throw", () => {
122+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
123+
root ??= createRoot(document.createElement("div"));
124+
act(() => root!.render(<CustomViewToolbar />));
125+
126+
expect(harness.renderedItems.map((item) => item.type)).toEqual(["custom"]);
127+
});

‎apps/mobile/src/native/StackHeader.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
NativeStackNavigationOptions,
44
NativeStackNavigationProp,
55
} from "@react-navigation/native-stack";
6-
import { use, useEffect, useLayoutEffect, useMemo, useRef } from "react";
6+
import { isValidElement, use, useEffect, useLayoutEffect, useMemo, useRef } from "react";
77
import { NativeWorkspaceTabActivityContext } from "./v5-workspace-context";
88
import type { ColorValue } from "react-native";
99

@@ -81,6 +81,13 @@ function optionsSignature(value: unknown, seen = new WeakSet<object>()): string
8181
// React refs carry mutable native instances that must not make static
8282
// screen options appear different after every render.
8383
if ("current" in object) return "[ref]";
84+
// Custom header views are elements. Only their type, key, and props describe them;
85+
// dev-only internals such as the owner fiber reach context defaults that throw.
86+
if (isValidElement(object)) {
87+
const type = object.type as { displayName?: string; name?: string } | string;
88+
const typeName = typeof type === "string" ? type : (type.displayName ?? type.name);
89+
return `element:${typeName}:${String(object.key)}:${optionsSignature(object.props, seen)}`;
90+
}
8491
return `{${Object.keys(value as Record<string, unknown>)
8592
.sort()
8693
.map(

0 commit comments

Comments
 (0)