fix(mobile): Stabilize native stack option updates (#4037)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
e8ff6bc7f8
commit
271454cbd2
3 files changed
+105
-6
No files matched your search
@@ -11,6 +11,7 @@ import {
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
type ReactElement,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
@@ -61,20 +62,116 @@ function normalizeScreenOptions(
|
||||
return normalized as NativeStackNavigationOptions;
|
||||
}
|
||||
|
||||
function optionsSignature(value: unknown, seen = new WeakSet<object>()): string {
|
||||
if (value === null) return "null";
|
||||
switch (typeof value) {
|
||||
case "boolean":
|
||||
case "number":
|
||||
case "string":
|
||||
return JSON.stringify(value);
|
||||
case "undefined":
|
||||
return "undefined";
|
||||
case "function":
|
||||
// Header factories are frequently recreated inline. Their source is
|
||||
// stable across equivalent renders, while a reference comparison would
|
||||
// make navigation.setOptions re-enter the navigator indefinitely.
|
||||
return `function:${Function.prototype.toString.call(value)}`;
|
||||
case "symbol":
|
||||
return `symbol:${String(value)}`;
|
||||
case "bigint":
|
||||
return `bigint:${String(value)}`;
|
||||
case "object": {
|
||||
const object = value as object;
|
||||
if (seen.has(object)) return "[circular]";
|
||||
seen.add(object);
|
||||
if (Array.isArray(value)) {
|
||||
return `[${value.map((entry) => optionsSignature(entry, seen)).join(",")}]`;
|
||||
}
|
||||
// React refs carry mutable native instances that must not make static
|
||||
// screen options appear different after every render.
|
||||
if ("current" in object) return "[ref]";
|
||||
return `{${Object.keys(value as Record<string, unknown>)
|
||||
.sort()
|
||||
.map(
|
||||
(key) =>
|
||||
`${JSON.stringify(key)}:${optionsSignature((value as Record<string, unknown>)[key], seen)}`,
|
||||
)
|
||||
.join(",")}}`;
|
||||
}
|
||||
}
|
||||
return String(value);
|
||||
}
|
||||
|
||||
function stabilizeOptionFunctions(
|
||||
value: unknown,
|
||||
path: string,
|
||||
latestFunctions: Map<string, (...args: unknown[]) => unknown>,
|
||||
wrappers: Map<string, (...args: unknown[]) => unknown>,
|
||||
seen = new WeakSet<object>(),
|
||||
): unknown {
|
||||
if (typeof value === "function") {
|
||||
latestFunctions.set(path, value as (...args: unknown[]) => unknown);
|
||||
let wrapper = wrappers.get(path);
|
||||
if (!wrapper) {
|
||||
wrapper = (...args: unknown[]) => {
|
||||
return latestFunctions.get(path)?.(...args);
|
||||
};
|
||||
wrappers.set(path, wrapper);
|
||||
}
|
||||
return wrapper;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
if (seen.has(value)) return value;
|
||||
seen.add(value);
|
||||
return value.map((entry, index) =>
|
||||
stabilizeOptionFunctions(entry, `${path}[${index}]`, latestFunctions, wrappers, seen),
|
||||
);
|
||||
}
|
||||
if (value !== null && typeof value === "object") {
|
||||
if (seen.has(value) || "current" in value) return value;
|
||||
seen.add(value);
|
||||
return Object.fromEntries(
|
||||
Object.entries(value as Record<string, unknown>).map(([key, entry]) => [
|
||||
key,
|
||||
stabilizeOptionFunctions(entry, `${path}.${key}`, latestFunctions, wrappers, seen),
|
||||
]),
|
||||
);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function NativeStackScreenOptions(props: {
|
||||
readonly options?: AppNativeStackNavigationOptions;
|
||||
readonly listeners?: Record<string, (event: never) => void>;
|
||||
readonly name?: string;
|
||||
}) {
|
||||
const navigation = useNativeStackNavigation();
|
||||
const lastAppliedOptionsSignatureRef = useRef<string | undefined>(undefined);
|
||||
const latestOptionFunctionsRef = useRef(new Map<string, (...args: unknown[]) => unknown>());
|
||||
const optionFunctionWrappersRef = useRef(new Map<string, (...args: unknown[]) => unknown>());
|
||||
const normalizedOptions = useMemo(() => normalizeScreenOptions(props.options), [props.options]);
|
||||
const stableOptions = normalizedOptions
|
||||
? (stabilizeOptionFunctions(
|
||||
normalizedOptions,
|
||||
"options",
|
||||
latestOptionFunctionsRef.current,
|
||||
optionFunctionWrappersRef.current,
|
||||
) as NativeStackNavigationOptions)
|
||||
: undefined;
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!navigation || !normalizedOptions) {
|
||||
if (!navigation || !stableOptions) {
|
||||
return;
|
||||
}
|
||||
navigation.setOptions(normalizedOptions);
|
||||
}, [navigation, normalizedOptions]);
|
||||
const signature = optionsSignature(stableOptions);
|
||||
// Avoid re-entering navigation state when semantically equal options are
|
||||
// reapplied every layout (common when callers pass unstable object literals).
|
||||
if (lastAppliedOptionsSignatureRef.current === signature) {
|
||||
return;
|
||||
}
|
||||
lastAppliedOptionsSignatureRef.current = signature;
|
||||
navigation.setOptions(stableOptions);
|
||||
}, [navigation, stableOptions]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!navigation || !props.listeners) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
diff --git a/lib/module/views/useHeaderConfigProps.js b/lib/module/views/useHeaderConfigProps.js
|
||||
index 0b75c70b4e0d233ee3b5faaf9cfbc40d4f8ed494..eb174e3fde91a7783f132b3fb16b01175ec19220 100644
|
||||
--- a/lib/module/views/useHeaderConfigProps.js
|
||||
+++ b/lib/module/views/useHeaderConfigProps.js
|
||||
@@ -19,6 +19,12 @@
|
||||
|
||||
Generated
+3
-3
@@ -75,7 +75,7 @@ patchedDependencies:
|
||||
'@legendapp/[email protected]': 45e4cbcbeeca1b628b8480869374d7cbc77e3fbfa97ee0107a0d72c3c4a5d7f3
|
||||
'@pierre/[email protected]': 7cb6da88544119adda056b2f46f43956f99326227732da0b345081e285a6c53a
|
||||
'@react-native-menu/[email protected]': 5ea3ae4bf1d9baf5443b65c269bb09621c27a68d556f713778f37b1e8d46aaae
|
||||
'@react-navigation/[email protected]': 2d7fec7933e324ccf082b1ae0df3907c35d16d48c117d628e27d9c3921c26bca
|
||||
'@react-navigation/[email protected]': c7fc101b78d434904425e5a24c22fb0042298dec6f807250486e784f3c717273
|
||||
[email protected]: c502bc684210b707dfceb87d8fe6ad6843395af6e19cfc02cd65854898bde2c5
|
||||
[email protected]: 9170f8074ae4e35a0a086e756c8f815794fd3abe51eac67ca3ba02804225ec1f
|
||||
[email protected]: 808eb26f9e57cf4945efd3985af4d9c764da6f91f4c9764433cc868602bbf4d3
|
||||
@@ -229,7 +229,7 @@ importers:
|
||||
version: 7.3.4([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected])
|
||||
'@react-navigation/native-stack':
|
||||
specifier: 7.17.6
|
||||
version: 7.17.6(patch_hash=2d7fec7933e324ccf082b1ae0df3907c35d16d48c117d628e27d9c3921c26bca)(a49e8e72dc3ef754b9d26038db8e6d3f)
|
||||
version: 7.17.6(patch_hash=c7fc101b78d434904425e5a24c22fb0042298dec6f807250486e784f3c717273)(a49e8e72dc3ef754b9d26038db8e6d3f)
|
||||
'@shikijs/core':
|
||||
specifier: 4.2.0
|
||||
version: 4.2.0
|
||||
@@ -14148,7 +14148,7 @@ snapshots:
|
||||
optionalDependencies:
|
||||
'@react-native-masked-view/masked-view': 0.3.2([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected])
|
||||
|
||||
'@react-navigation/[email protected](patch_hash=2d7fec7933e324ccf082b1ae0df3907c35d16d48c117d628e27d9c3921c26bca)(a49e8e72dc3ef754b9d26038db8e6d3f)':
|
||||
'@react-navigation/[email protected](patch_hash=c7fc101b78d434904425e5a24c22fb0042298dec6f807250486e784f3c717273)(a49e8e72dc3ef754b9d26038db8e6d3f)':
|
||||
dependencies:
|
||||
'@react-navigation/elements': 2.9.26(c6a2ad0e2c930f8e3896e77c3ba11bc4)
|
||||
'@react-navigation/native': 7.3.4([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected]))([email protected])
|
||||
|
||||
Reference in new issue
Block a user