fix(mobile): Stabilize native stack option updates #4037
Mergedjuliusmarmingemerged 6 commits into
main fromrefs/pull/4037/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
- apps/mobile/src/native/StackHeader.tsx line 80OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -67,9 +68,13 @@6768 readonly name?: string;6869 }) {6970 const navigation = useNativeStackNavigation();71+ const lastAppliedOptionsRef = useRef<NativeStackNavigationOptions | undefined>(undefined);7072 const normalizedOptions = useMemo(() => normalizeScreenOptions(props.options), [props.options]);71737274 useLayoutEffect(() => {7375 if (!navigation || !normalizedOptions) {7476 return;7577 }78+ // Avoid re-entering navigation state when the same options object is79+ // reapplied every layout (common when callers pass unstable object literals).80+ if (lastAppliedOptionsRef.current === normalizedOptions) {
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/mobile/src/native/StackHeader.tsx line 78Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -61,3 +62,17 @@6162 return normalized as NativeStackNavigationOptions;6263 }636465+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 is76+ // stable across equivalent renders, while a reference comparison would77+ // make navigation.setOptions re-enter the navigator indefinitely.78+ return `function:${Function.prototype.toString.call(value)}`;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -63,0 +85,21 @@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 static91+ // 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+105+function stabilizeOptionFunctions(
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge merged this271454c
juliusmarminge closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID:
a3ce3425-2f1f-4574-9470-3c9c6a9ce047You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.