-
-
Notifications
You must be signed in to change notification settings - Fork 643
Expand file tree
/
Copy pathStackContainer.tsx
More file actions
125 lines (111 loc) · 3.95 KB
/
StackContainer.tsx
File metadata and controls
125 lines (111 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React from 'react';
import { Stack } from 'react-native-screens/experimental';
import type {
NavigationAction,
StackContainerProps,
StackNavigationState,
StackRouteConfig,
} from './StackContainer.types';
import {
determineInitialNavigationState,
navigationStateReducerWithLogging,
} from './reducer';
import { useStackOperationMethods } from './hooks/useStackOperationMethods';
import {
StackNavigationContext,
type StackNavigationContextPayload,
} from './contexts/StackNavigationContext';
import {
type NativeComponentGenericRef,
RNSLog,
useRenderDebugInfo,
} from 'react-native-screens/private';
import { useParentNavigationEffect } from './hooks/useParentNavigationEffect';
import { useComponentsByName } from '../shared/use-components-by-name';
export function StackContainer({ routeConfigs }: StackContainerProps) {
useSanitizeRouteConfigs(routeConfigs);
const componentsByName = useComponentsByName(routeConfigs);
const [stackNavState, navActionDispatch]: [
StackNavigationState,
React.Dispatch<NavigationAction>,
] = React.useReducer(
navigationStateReducerWithLogging,
routeConfigs,
determineInitialNavigationState,
);
const navMethods = useStackOperationMethods(navActionDispatch, routeConfigs);
// If reducer produced a parent action, we need to dispatch it
// as an effect, because we can not modify the state during the render phase.
useParentNavigationEffect(navMethods, stackNavState.effects);
const hostRef =
useRenderDebugInfo<NativeComponentGenericRef>('StackContainer');
const onScreenDismissed = React.useCallback(
(screenKey: string) => {
RNSLog.log(`onScreenDismissed for ${screenKey}`);
navMethods.popCompletedAction(screenKey);
},
[navMethods],
);
const onScreenNativelyDismissed = React.useCallback(
(screenKey: string) => {
RNSLog.log(`onScreenNativelyDismissed for ${screenKey}`);
navMethods.popNativeAction(screenKey);
},
[navMethods],
);
return (
<Stack.Host ref={hostRef}>
{stackNavState.stack.map(
({ options: { headerConfig, ...options }, activityMode, routeKey, name }) => {
const stackNavigationContext: StackNavigationContextPayload = {
routeKey,
routeOptions: { ...options },
push: navMethods.pushAction,
pop: navMethods.popAction,
preload: navMethods.preloadAction,
batch: navMethods.batchAction,
setRouteOptions: navMethods.setRouteOptions,
};
const Component = componentsByName.get(name);
if (!Component) {
throw new Error(
`[Stack] No config matches the "${name}" route name`,
);
}
return (
<Stack.Screen
key={routeKey}
{...options}
activityMode={activityMode}
screenKey={routeKey}
onDismiss={onScreenDismissed}
onNativeDismiss={onScreenNativelyDismissed}>
<StackNavigationContext.Provider value={stackNavigationContext}>
<Component />
{headerConfig !== undefined && (
<Stack.HeaderConfig {...headerConfig} />
)}
</StackNavigationContext.Provider>
</Stack.Screen>
);
},
)}
</Stack.Host>
);
}
function useSanitizeRouteConfigs(
routeConfigs?: StackRouteConfig[] | undefined | null,
) {
if (!routeConfigs || routeConfigs.length === 0) {
throw new Error('[Stack] There must be at least one route configured');
}
// Do not recompute in case the routeConfigs have not changed
const areNamesUnique = React.useMemo(() => {
const routeNames = routeConfigs.map(routeConfig => routeConfig.name);
const uniqueRouteNames = new Set(routeNames);
return routeNames.length === uniqueRouteNames.size;
}, [routeConfigs]);
if (!areNamesUnique) {
throw new Error('[Stack] All routes must have unique names');
}
}