Skip to content

Commit f356466

Browse files
juliusmarmingecodex
andcommitted
fix(mobile): Compare native stack options structurally
Co-authored-by: codex <[email protected]>
1 parent f4457a8 commit f356466

1 file changed

Lines changed: 45 additions & 4 deletions

File tree

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,66 @@ function normalizeScreenOptions(
6262
return normalized as NativeStackNavigationOptions;
6363
}
6464

65+
function optionsSignature(value: unknown, seen = new WeakSet<object>()): string {
66+
if (value === null) return "null";
67+
switch (typeof value) {
68+
case "boolean":
69+
case "number":
70+
case "string":
71+
return JSON.stringify(value);
72+
case "undefined":
73+
return "undefined";
74+
case "function":
75+
// Header factories are frequently recreated inline. Their source is
76+
// stable across equivalent renders, while a reference comparison would
77+
// make navigation.setOptions re-enter the navigator indefinitely.
78+
return `function:${Function.prototype.toString.call(value)}`;
79+
case "symbol":
80+
return `symbol:${String(value)}`;
81+
case "bigint":
82+
return `bigint:${String(value)}`;
83+
case "object": {
84+
const object = value as object;
85+
if (seen.has(object)) return "[circular]";
86+
seen.add(object);
87+
if (Array.isArray(value)) {
88+
return `[${value.map((entry) => optionsSignature(entry, seen)).join(",")}]`;
89+
}
90+
// React refs carry mutable native instances that must not make static
91+
// screen options appear different after every render.
92+
if ("current" in object) return "[ref]";
93+
return `{${Object.keys(value as Record<string, unknown>)
94+
.sort()
95+
.map(
96+
(key) =>
97+
`${JSON.stringify(key)}:${optionsSignature((value as Record<string, unknown>)[key], seen)}`,
98+
)
99+
.join(",")}}`;
100+
}
101+
}
102+
return String(value);
103+
}
104+
65105
export function NativeStackScreenOptions(props: {
66106
readonly options?: AppNativeStackNavigationOptions;
67107
readonly listeners?: Record<string, (event: never) => void>;
68108
readonly name?: string;
69109
}) {
70110
const navigation = useNativeStackNavigation();
71-
const lastAppliedOptionsRef = useRef<NativeStackNavigationOptions | undefined>(undefined);
111+
const lastAppliedOptionsSignatureRef = useRef<string | undefined>(undefined);
72112
const normalizedOptions = useMemo(() => normalizeScreenOptions(props.options), [props.options]);
73113

74114
useLayoutEffect(() => {
75115
if (!navigation || !normalizedOptions) {
76116
return;
77117
}
78-
// Avoid re-entering navigation state when the same options object is
118+
const signature = optionsSignature(normalizedOptions);
119+
// Avoid re-entering navigation state when semantically equal options are
79120
// reapplied every layout (common when callers pass unstable object literals).
80-
if (lastAppliedOptionsRef.current === normalizedOptions) {
121+
if (lastAppliedOptionsSignatureRef.current === signature) {
81122
return;
82123
}
83-
lastAppliedOptionsRef.current = normalizedOptions;
124+
lastAppliedOptionsSignatureRef.current = signature;
84125
navigation.setOptions(normalizedOptions);
85126
}, [navigation, normalizedOptions]);
86127

0 commit comments

Comments
 (0)