@react-navigation/core 8.0.0-alpha.1 → 8.0.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/module/BaseNavigationContainer.js +14 -7
- package/lib/module/BaseNavigationContainer.js.map +1 -1
- package/lib/module/ConsumedParamsContext.js +5 -0
- package/lib/module/ConsumedParamsContext.js.map +1 -0
- package/lib/module/NavigationBuilderContext.js +1 -0
- package/lib/module/NavigationBuilderContext.js.map +1 -1
- package/lib/module/NavigationIndependentTree.js +8 -4
- package/lib/module/NavigationIndependentTree.js.map +1 -1
- package/lib/module/NavigationProvider.js +14 -3
- package/lib/module/NavigationProvider.js.map +1 -1
- package/lib/module/NavigationStateContext.js.map +1 -1
- package/lib/module/PreventRemoveProvider.js +3 -3
- package/lib/module/PreventRemoveProvider.js.map +1 -1
- package/lib/module/SceneView.js +46 -56
- package/lib/module/SceneView.js.map +1 -1
- package/lib/module/StaticNavigation.js +34 -4
- package/lib/module/StaticNavigation.js.map +1 -1
- package/lib/module/createNavigatorFactory.js +22 -1
- package/lib/module/createNavigatorFactory.js.map +1 -1
- package/lib/module/getPathFromState.js +25 -3
- package/lib/module/getPathFromState.js.map +1 -1
- package/lib/module/getStateFromPath.js +158 -73
- package/lib/module/getStateFromPath.js.map +1 -1
- package/lib/module/getStateFromRouteParams.js +24 -0
- package/lib/module/getStateFromRouteParams.js.map +1 -0
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/theming/useTheme.js +1 -1
- package/lib/module/theming/useTheme.js.map +1 -1
- package/lib/module/types.js.map +1 -1
- package/lib/module/useCurrentRender.js +1 -1
- package/lib/module/useCurrentRender.js.map +1 -1
- package/lib/module/useDescriptors.js +12 -43
- package/lib/module/useDescriptors.js.map +1 -1
- package/lib/module/useFocusEvents.js +1 -1
- package/lib/module/useFocusEvents.js.map +1 -1
- package/lib/module/useFocusedListenersChildrenAdapter.js +1 -1
- package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -1
- package/lib/module/useIsFocused.js +7 -12
- package/lib/module/useIsFocused.js.map +1 -1
- package/lib/module/useNavigationBuilder.js +84 -38
- package/lib/module/useNavigationBuilder.js.map +1 -1
- package/lib/module/useNavigationCache.js +14 -56
- package/lib/module/useNavigationCache.js.map +1 -1
- package/lib/module/useNavigationHelpers.js +1 -1
- package/lib/module/useNavigationHelpers.js.map +1 -1
- package/lib/module/useNavigationIndependentTree.js +1 -1
- package/lib/module/useNavigationIndependentTree.js.map +1 -1
- package/lib/module/useNavigationState.js +4 -2
- package/lib/module/useNavigationState.js.map +1 -1
- package/lib/module/useOnAction.js +1 -1
- package/lib/module/useOnAction.js.map +1 -1
- package/lib/module/useOnGetState.js +2 -2
- package/lib/module/useOnGetState.js.map +1 -1
- package/lib/module/useOnPreventRemove.js +2 -2
- package/lib/module/useOnPreventRemove.js.map +1 -1
- package/lib/module/useOnRouteFocus.js +1 -1
- package/lib/module/useOnRouteFocus.js.map +1 -1
- package/lib/module/useOptionsGetters.js +2 -2
- package/lib/module/useOptionsGetters.js.map +1 -1
- package/lib/module/usePreventRemoveContext.js +1 -1
- package/lib/module/usePreventRemoveContext.js.map +1 -1
- package/lib/module/useRegisterNavigator.js +1 -1
- package/lib/module/useRegisterNavigator.js.map +1 -1
- package/lib/module/useScheduleUpdate.js +1 -1
- package/lib/module/useScheduleUpdate.js.map +1 -1
- package/lib/module/useStateForPath.js +1 -1
- package/lib/module/useStateForPath.js.map +1 -1
- package/lib/typescript/src/BaseNavigationContainer.d.ts +6 -3
- package/lib/typescript/src/BaseNavigationContainer.d.ts.map +1 -1
- package/lib/typescript/src/ConsumedParamsContext.d.ts +8 -0
- package/lib/typescript/src/ConsumedParamsContext.d.ts.map +1 -0
- package/lib/typescript/src/NavigationBuilderContext.d.ts +11 -5
- package/lib/typescript/src/NavigationBuilderContext.d.ts.map +1 -1
- package/lib/typescript/src/NavigationFocusedRouteStateContext.d.ts +4 -4
- package/lib/typescript/src/NavigationFocusedRouteStateContext.d.ts.map +1 -1
- package/lib/typescript/src/NavigationIndependentTree.d.ts.map +1 -1
- package/lib/typescript/src/NavigationProvider.d.ts +4 -4
- package/lib/typescript/src/NavigationProvider.d.ts.map +1 -1
- package/lib/typescript/src/NavigationStateContext.d.ts +3 -3
- package/lib/typescript/src/NavigationStateContext.d.ts.map +1 -1
- package/lib/typescript/src/SceneView.d.ts.map +1 -1
- package/lib/typescript/src/StaticContainer.d.ts +1 -1
- package/lib/typescript/src/StaticContainer.d.ts.map +1 -1
- package/lib/typescript/src/StaticNavigation.d.ts +39 -24
- package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
- package/lib/typescript/src/createNavigatorFactory.d.ts +1 -1
- package/lib/typescript/src/createNavigatorFactory.d.ts.map +1 -1
- package/lib/typescript/src/findFocusedRoute.d.ts +3 -3
- package/lib/typescript/src/findFocusedRoute.d.ts.map +1 -1
- package/lib/typescript/src/getActionFromState.d.ts +3 -3
- package/lib/typescript/src/getActionFromState.d.ts.map +1 -1
- package/lib/typescript/src/getPathFromState.d.ts +2 -2
- package/lib/typescript/src/getPathFromState.d.ts.map +1 -1
- package/lib/typescript/src/getStateFromPath.d.ts +3 -3
- package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
- package/lib/typescript/src/getStateFromRouteParams.d.ts +3 -0
- package/lib/typescript/src/getStateFromRouteParams.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +144 -82
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/useDescriptors.d.ts +12 -25
- package/lib/typescript/src/useDescriptors.d.ts.map +1 -1
- package/lib/typescript/src/useIsFocused.d.ts +3 -0
- package/lib/typescript/src/useIsFocused.d.ts.map +1 -1
- package/lib/typescript/src/useNavigation.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationBuilder.d.ts +20 -29
- package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationCache.d.ts +2 -42
- package/lib/typescript/src/useNavigationCache.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationHelpers.d.ts +18 -16
- package/lib/typescript/src/useNavigationHelpers.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationState.d.ts.map +1 -1
- package/lib/typescript/src/useOnAction.d.ts +6 -6
- package/lib/typescript/src/useOnAction.d.ts.map +1 -1
- package/lib/typescript/src/useOnRouteFocus.d.ts +6 -6
- package/lib/typescript/src/useOnRouteFocus.d.ts.map +1 -1
- package/lib/typescript/src/useRouteCache.d.ts +2 -2
- package/lib/typescript/src/useScheduleUpdate.d.ts.map +1 -1
- package/lib/typescript/src/utilities.d.ts +42 -3
- package/lib/typescript/src/utilities.d.ts.map +1 -1
- package/package.json +20 -18
- package/src/BaseNavigationContainer.tsx +332 -326
- package/src/ConsumedParamsContext.tsx +10 -0
- package/src/NavigationBuilderContext.tsx +14 -8
- package/src/NavigationFocusedRouteStateContext.tsx +4 -4
- package/src/NavigationIndependentTree.tsx +8 -5
- package/src/NavigationProvider.tsx +17 -3
- package/src/NavigationStateContext.tsx +5 -6
- package/src/PreventRemoveProvider.tsx +3 -3
- package/src/SceneView.tsx +58 -56
- package/src/StaticNavigation.tsx +121 -51
- package/src/createNavigatorFactory.tsx +35 -4
- package/src/findFocusedRoute.tsx +3 -3
- package/src/getActionFromState.tsx +7 -7
- package/src/getPathFromState.tsx +53 -9
- package/src/getStateFromPath.tsx +255 -97
- package/src/getStateFromRouteParams.tsx +60 -0
- package/src/index.tsx +1 -1
- package/src/theming/useTheme.tsx +1 -1
- package/src/types.tsx +330 -187
- package/src/useCurrentRender.tsx +1 -1
- package/src/useDescriptors.tsx +13 -48
- package/src/useFocusEvents.tsx +1 -1
- package/src/useFocusedListenersChildrenAdapter.tsx +1 -1
- package/src/useIsFocused.tsx +14 -21
- package/src/useNavigation.tsx +2 -2
- package/src/useNavigationBuilder.tsx +131 -49
- package/src/useNavigationCache.tsx +12 -73
- package/src/useNavigationHelpers.tsx +1 -1
- package/src/useNavigationIndependentTree.tsx +1 -1
- package/src/useNavigationState.tsx +6 -4
- package/src/useOnAction.tsx +8 -8
- package/src/useOnGetState.tsx +2 -2
- package/src/useOnPreventRemove.tsx +2 -2
- package/src/useOnRouteFocus.tsx +10 -12
- package/src/useOptionsGetters.tsx +2 -2
- package/src/usePreventRemoveContext.tsx +1 -1
- package/src/useRegisterNavigator.tsx +1 -1
- package/src/useScheduleUpdate.tsx +1 -3
- package/src/useStateForPath.tsx +1 -1
- package/src/utilities.tsx +94 -4
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
type InitialState,
|
|
4
4
|
type NavigationAction,
|
|
5
5
|
type NavigationState,
|
|
6
|
-
type ParamListBase,
|
|
7
6
|
type PartialState,
|
|
8
7
|
type Route,
|
|
9
8
|
} from '@react-navigation/routers';
|
|
@@ -24,6 +23,7 @@ import type {
|
|
|
24
23
|
NavigationContainerEventMap,
|
|
25
24
|
NavigationContainerProps,
|
|
26
25
|
NavigationContainerRef,
|
|
26
|
+
RootParamList,
|
|
27
27
|
} from './types';
|
|
28
28
|
import { UnhandledActionContext } from './UnhandledActionContext';
|
|
29
29
|
import { useChildListeners } from './useChildListeners';
|
|
@@ -38,6 +38,10 @@ type State = NavigationState | PartialState<NavigationState> | undefined;
|
|
|
38
38
|
const serializableWarnings: string[] = [];
|
|
39
39
|
const duplicateNameWarnings: string[] = [];
|
|
40
40
|
|
|
41
|
+
type Props<ParamList extends {}> = NavigationContainerProps & {
|
|
42
|
+
ref?: React.Ref<NavigationContainerRef<ParamList>>;
|
|
43
|
+
};
|
|
44
|
+
|
|
41
45
|
/**
|
|
42
46
|
* Remove `key` and `routeNames` from the state objects recursively to get partial state.
|
|
43
47
|
*
|
|
@@ -80,382 +84,384 @@ const getPartialState = (
|
|
|
80
84
|
* @param props.children Child elements to render the content.
|
|
81
85
|
* @param props.ref Ref object which refers to the navigation object containing helper methods.
|
|
82
86
|
*/
|
|
83
|
-
export
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
"Looks like you have nested a 'NavigationContainer' inside another. Normally you need only one container at the root of the app, so this was probably an error. If this was intentional, wrap the container in 'NavigationIndependentTree' explicitly. Note that this will make the child navigators disconnected from the parent and you won't be able to navigate between them."
|
|
101
|
-
);
|
|
102
|
-
}
|
|
87
|
+
export function BaseNavigationContainer<ParamList extends {} = RootParamList>({
|
|
88
|
+
initialState,
|
|
89
|
+
onStateChange,
|
|
90
|
+
onReady,
|
|
91
|
+
onUnhandledAction,
|
|
92
|
+
theme,
|
|
93
|
+
children,
|
|
94
|
+
ref,
|
|
95
|
+
}: Props<ParamList>) {
|
|
96
|
+
const parent = React.use(NavigationStateContext);
|
|
97
|
+
const independent = useNavigationIndependentTree();
|
|
98
|
+
|
|
99
|
+
if (!parent.isDefault && !independent) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
"Looks like you have nested a 'NavigationContainer' inside another. Normally you need only one container at the root of the app, so this was probably an error. If this was intentional, wrap the container in 'NavigationIndependentTree' explicitly. Note that this will make the child navigators disconnected from the parent and you won't be able to navigate between them."
|
|
102
|
+
);
|
|
103
|
+
}
|
|
103
104
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
const { state, getState, setState, scheduleUpdate, flushUpdates } =
|
|
106
|
+
useSyncState<State>(() =>
|
|
107
|
+
getPartialState(initialState == null ? undefined : initialState)
|
|
108
|
+
);
|
|
108
109
|
|
|
109
|
-
|
|
110
|
+
const isFirstMountRef = React.useRef<boolean>(true);
|
|
110
111
|
|
|
111
|
-
|
|
112
|
+
const navigatorKeyRef = React.useRef<string | undefined>(undefined);
|
|
112
113
|
|
|
113
|
-
|
|
114
|
+
const getKey = React.useCallback(() => navigatorKeyRef.current, []);
|
|
114
115
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
const setKey = React.useCallback((key: string) => {
|
|
117
|
+
navigatorKeyRef.current = key;
|
|
118
|
+
}, []);
|
|
118
119
|
|
|
119
|
-
|
|
120
|
+
const { listeners, addListener } = useChildListeners();
|
|
120
121
|
|
|
121
|
-
|
|
122
|
+
const { keyedListeners, addKeyedListener } = useKeyedChildListeners();
|
|
122
123
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
} else {
|
|
132
|
-
listeners.focus[0]((navigation) => navigation.dispatch(action));
|
|
133
|
-
}
|
|
124
|
+
const dispatch = useLatestCallback(
|
|
125
|
+
(
|
|
126
|
+
action: NavigationAction | ((state: NavigationState) => NavigationAction)
|
|
127
|
+
) => {
|
|
128
|
+
if (listeners.focus[0] == null) {
|
|
129
|
+
console.error(NOT_INITIALIZED_ERROR);
|
|
130
|
+
} else {
|
|
131
|
+
listeners.focus[0]((navigation) => navigation.dispatch(action));
|
|
134
132
|
}
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const canGoBack = useLatestCallback(() => {
|
|
137
|
+
if (listeners.focus[0] == null) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const { result, handled } = listeners.focus[0]((navigation) =>
|
|
142
|
+
navigation.canGoBack()
|
|
135
143
|
);
|
|
136
144
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
145
|
+
if (handled) {
|
|
146
|
+
return result;
|
|
147
|
+
} else {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
141
151
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
);
|
|
152
|
+
const resetRoot = useLatestCallback(
|
|
153
|
+
(state: PartialState<NavigationState> | NavigationState) => {
|
|
154
|
+
const target = state?.key ?? keyedListeners.getState.root?.().key;
|
|
145
155
|
|
|
146
|
-
if (
|
|
147
|
-
|
|
156
|
+
if (target == null) {
|
|
157
|
+
console.error(NOT_INITIALIZED_ERROR);
|
|
148
158
|
} else {
|
|
149
|
-
|
|
159
|
+
listeners.focus[0]((navigation) =>
|
|
160
|
+
navigation.dispatch({
|
|
161
|
+
...CommonActions.reset(state),
|
|
162
|
+
target,
|
|
163
|
+
})
|
|
164
|
+
);
|
|
150
165
|
}
|
|
151
|
-
}
|
|
166
|
+
}
|
|
167
|
+
);
|
|
152
168
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (target == null) {
|
|
158
|
-
console.error(NOT_INITIALIZED_ERROR);
|
|
159
|
-
} else {
|
|
160
|
-
listeners.focus[0]((navigation) =>
|
|
161
|
-
navigation.dispatch({
|
|
162
|
-
...CommonActions.reset(state),
|
|
163
|
-
target,
|
|
164
|
-
})
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
);
|
|
169
|
+
const getRootState = useLatestCallback(() => {
|
|
170
|
+
return keyedListeners.getState.root?.();
|
|
171
|
+
});
|
|
169
172
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
});
|
|
173
|
+
const getCurrentRoute = useLatestCallback(() => {
|
|
174
|
+
const state = getRootState();
|
|
173
175
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
+
if (state == null) {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
176
179
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
+
const route = findFocusedRoute(state);
|
|
181
|
+
|
|
182
|
+
return route as Route<string> | undefined;
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const isReady = useLatestCallback(() => listeners.focus[0] != null);
|
|
186
|
+
|
|
187
|
+
const emitter = useEventEmitter<NavigationContainerEventMap>();
|
|
188
|
+
|
|
189
|
+
const { addOptionsGetter, getCurrentOptions } = useOptionsGetters({});
|
|
190
|
+
|
|
191
|
+
const navigation: NavigationContainerRef<ParamList> = React.useMemo(
|
|
192
|
+
() => ({
|
|
193
|
+
...Object.keys(CommonActions).reduce<any>((acc, name) => {
|
|
194
|
+
acc[name] = (...args: any[]) =>
|
|
195
|
+
// @ts-expect-error: this is ok
|
|
196
|
+
dispatch(CommonActions[name](...args));
|
|
197
|
+
return acc;
|
|
198
|
+
}, {}),
|
|
199
|
+
...emitter.create('root'),
|
|
200
|
+
dispatch,
|
|
201
|
+
resetRoot,
|
|
202
|
+
isFocused: () => true,
|
|
203
|
+
canGoBack,
|
|
204
|
+
getParent: () => undefined,
|
|
205
|
+
getState,
|
|
206
|
+
getRootState,
|
|
207
|
+
getCurrentRoute,
|
|
208
|
+
getCurrentOptions,
|
|
209
|
+
isReady,
|
|
210
|
+
setOptions: () => {
|
|
211
|
+
throw new Error('Cannot call setOptions outside a screen');
|
|
212
|
+
},
|
|
213
|
+
}),
|
|
214
|
+
[
|
|
215
|
+
canGoBack,
|
|
216
|
+
dispatch,
|
|
217
|
+
emitter,
|
|
218
|
+
getCurrentOptions,
|
|
219
|
+
getCurrentRoute,
|
|
220
|
+
getRootState,
|
|
221
|
+
getState,
|
|
222
|
+
isReady,
|
|
223
|
+
resetRoot,
|
|
224
|
+
]
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
React.useImperativeHandle(ref, () => navigation, [navigation]);
|
|
228
|
+
|
|
229
|
+
const onDispatchAction = useLatestCallback(
|
|
230
|
+
(action: NavigationAction, noop: boolean) => {
|
|
231
|
+
emitter.emit({
|
|
232
|
+
type: '__unsafe_action__',
|
|
233
|
+
data: { action, noop, stack: stackRef.current },
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
const onEmitEvent = useLatestCallback(
|
|
239
|
+
(event: {
|
|
240
|
+
type: string;
|
|
241
|
+
defaultPrevented: boolean | undefined;
|
|
242
|
+
target: string | undefined;
|
|
243
|
+
data: unknown;
|
|
244
|
+
}) => {
|
|
245
|
+
emitter.emit({
|
|
246
|
+
type: '__unsafe_event__',
|
|
247
|
+
data: event,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
);
|
|
180
251
|
|
|
181
|
-
|
|
252
|
+
const lastEmittedOptionsRef = React.useRef<object | undefined>(undefined);
|
|
182
253
|
|
|
183
|
-
|
|
184
|
-
|
|
254
|
+
const onOptionsChange = useLatestCallback((options: object) => {
|
|
255
|
+
if (lastEmittedOptionsRef.current === options) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
185
258
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const emitter = useEventEmitter<NavigationContainerEventMap>();
|
|
189
|
-
|
|
190
|
-
const { addOptionsGetter, getCurrentOptions } = useOptionsGetters({});
|
|
191
|
-
|
|
192
|
-
const navigation: NavigationContainerRef<ParamListBase> = React.useMemo(
|
|
193
|
-
() => ({
|
|
194
|
-
...Object.keys(CommonActions).reduce<any>((acc, name) => {
|
|
195
|
-
acc[name] = (...args: any[]) =>
|
|
196
|
-
// @ts-expect-error: this is ok
|
|
197
|
-
dispatch(CommonActions[name](...args));
|
|
198
|
-
return acc;
|
|
199
|
-
}, {}),
|
|
200
|
-
...emitter.create('root'),
|
|
201
|
-
dispatch,
|
|
202
|
-
resetRoot,
|
|
203
|
-
isFocused: () => true,
|
|
204
|
-
canGoBack,
|
|
205
|
-
getParent: () => undefined,
|
|
206
|
-
getState,
|
|
207
|
-
getRootState,
|
|
208
|
-
getCurrentRoute,
|
|
209
|
-
getCurrentOptions,
|
|
210
|
-
isReady,
|
|
211
|
-
setOptions: () => {
|
|
212
|
-
throw new Error('Cannot call setOptions outside a screen');
|
|
213
|
-
},
|
|
214
|
-
}),
|
|
215
|
-
[
|
|
216
|
-
canGoBack,
|
|
217
|
-
dispatch,
|
|
218
|
-
emitter,
|
|
219
|
-
getCurrentOptions,
|
|
220
|
-
getCurrentRoute,
|
|
221
|
-
getRootState,
|
|
222
|
-
getState,
|
|
223
|
-
isReady,
|
|
224
|
-
resetRoot,
|
|
225
|
-
]
|
|
226
|
-
);
|
|
259
|
+
lastEmittedOptionsRef.current = options;
|
|
227
260
|
|
|
228
|
-
|
|
261
|
+
emitter.emit({
|
|
262
|
+
type: 'options',
|
|
263
|
+
data: { options },
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const stackRef = React.useRef<string | undefined>(undefined);
|
|
268
|
+
|
|
269
|
+
const builderContext = React.useMemo(
|
|
270
|
+
() => ({
|
|
271
|
+
addListener,
|
|
272
|
+
addKeyedListener,
|
|
273
|
+
onDispatchAction,
|
|
274
|
+
onEmitEvent,
|
|
275
|
+
onOptionsChange,
|
|
276
|
+
scheduleUpdate,
|
|
277
|
+
flushUpdates,
|
|
278
|
+
stackRef,
|
|
279
|
+
}),
|
|
280
|
+
[
|
|
281
|
+
addListener,
|
|
282
|
+
addKeyedListener,
|
|
283
|
+
onDispatchAction,
|
|
284
|
+
onEmitEvent,
|
|
285
|
+
onOptionsChange,
|
|
286
|
+
scheduleUpdate,
|
|
287
|
+
flushUpdates,
|
|
288
|
+
]
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
const isInitialRef = React.useRef(true);
|
|
292
|
+
|
|
293
|
+
const getIsInitial = React.useCallback(() => isInitialRef.current, []);
|
|
294
|
+
|
|
295
|
+
const context = React.useMemo(
|
|
296
|
+
() => ({
|
|
297
|
+
state,
|
|
298
|
+
getState,
|
|
299
|
+
setState,
|
|
300
|
+
getKey,
|
|
301
|
+
setKey,
|
|
302
|
+
getIsInitial,
|
|
303
|
+
addOptionsGetter,
|
|
304
|
+
}),
|
|
305
|
+
[state, getState, setState, getKey, setKey, getIsInitial, addOptionsGetter]
|
|
306
|
+
);
|
|
229
307
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
emitter.emit({
|
|
233
|
-
type: '__unsafe_action__',
|
|
234
|
-
data: { action, noop, stack: stackRef.current },
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
);
|
|
308
|
+
const onReadyRef = React.useRef(onReady);
|
|
309
|
+
const onStateChangeRef = React.useRef(onStateChange);
|
|
238
310
|
|
|
239
|
-
|
|
311
|
+
React.useEffect(() => {
|
|
312
|
+
isInitialRef.current = false;
|
|
313
|
+
onStateChangeRef.current = onStateChange;
|
|
314
|
+
onReadyRef.current = onReady;
|
|
315
|
+
});
|
|
240
316
|
|
|
241
|
-
|
|
242
|
-
if (lastEmittedOptionsRef.current === options) {
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
317
|
+
const onReadyCalledRef = React.useRef(false);
|
|
245
318
|
|
|
246
|
-
|
|
319
|
+
React.useEffect(() => {
|
|
320
|
+
if (!onReadyCalledRef.current && isReady()) {
|
|
321
|
+
onReadyCalledRef.current = true;
|
|
322
|
+
onReadyRef.current?.();
|
|
323
|
+
emitter.emit({ type: 'ready' });
|
|
324
|
+
}
|
|
325
|
+
}, [state, isReady, emitter]);
|
|
247
326
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
data: { options },
|
|
251
|
-
});
|
|
252
|
-
});
|
|
327
|
+
React.useEffect(() => {
|
|
328
|
+
const hydratedState = getRootState();
|
|
253
329
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
() => ({
|
|
258
|
-
addListener,
|
|
259
|
-
addKeyedListener,
|
|
260
|
-
onDispatchAction,
|
|
261
|
-
onOptionsChange,
|
|
262
|
-
scheduleUpdate,
|
|
263
|
-
flushUpdates,
|
|
264
|
-
stackRef,
|
|
265
|
-
}),
|
|
266
|
-
[
|
|
267
|
-
addListener,
|
|
268
|
-
addKeyedListener,
|
|
269
|
-
onDispatchAction,
|
|
270
|
-
onOptionsChange,
|
|
271
|
-
scheduleUpdate,
|
|
272
|
-
flushUpdates,
|
|
273
|
-
]
|
|
274
|
-
);
|
|
330
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
331
|
+
if (hydratedState !== undefined) {
|
|
332
|
+
const serializableResult = checkSerializable(hydratedState);
|
|
275
333
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
const getIsInitial = React.useCallback(() => isInitialRef.current, []);
|
|
279
|
-
|
|
280
|
-
const context = React.useMemo(
|
|
281
|
-
() => ({
|
|
282
|
-
state,
|
|
283
|
-
getState,
|
|
284
|
-
setState,
|
|
285
|
-
getKey,
|
|
286
|
-
setKey,
|
|
287
|
-
getIsInitial,
|
|
288
|
-
addOptionsGetter,
|
|
289
|
-
}),
|
|
290
|
-
[
|
|
291
|
-
state,
|
|
292
|
-
getState,
|
|
293
|
-
setState,
|
|
294
|
-
getKey,
|
|
295
|
-
setKey,
|
|
296
|
-
getIsInitial,
|
|
297
|
-
addOptionsGetter,
|
|
298
|
-
]
|
|
299
|
-
);
|
|
334
|
+
if (!serializableResult.serializable) {
|
|
335
|
+
const { location, reason } = serializableResult;
|
|
300
336
|
|
|
301
|
-
|
|
302
|
-
|
|
337
|
+
let path = '';
|
|
338
|
+
let pointer: Record<any, any> = hydratedState;
|
|
339
|
+
let params = false;
|
|
303
340
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
onReadyRef.current = onReady;
|
|
308
|
-
});
|
|
341
|
+
for (let i = 0; i < location.length; i++) {
|
|
342
|
+
const curr = location[i];
|
|
343
|
+
const prev = location[i - 1];
|
|
309
344
|
|
|
310
|
-
|
|
345
|
+
pointer = pointer[curr];
|
|
311
346
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
let params = false;
|
|
333
|
-
|
|
334
|
-
for (let i = 0; i < location.length; i++) {
|
|
335
|
-
const curr = location[i];
|
|
336
|
-
const prev = location[i - 1];
|
|
337
|
-
|
|
338
|
-
pointer = pointer[curr];
|
|
339
|
-
|
|
340
|
-
if (!params && curr === 'state') {
|
|
341
|
-
continue;
|
|
342
|
-
} else if (!params && curr === 'routes') {
|
|
343
|
-
if (path) {
|
|
344
|
-
path += ' > ';
|
|
345
|
-
}
|
|
346
|
-
} else if (
|
|
347
|
-
!params &&
|
|
348
|
-
typeof curr === 'number' &&
|
|
349
|
-
prev === 'routes'
|
|
350
|
-
) {
|
|
351
|
-
path += pointer?.name;
|
|
352
|
-
} else if (!params) {
|
|
353
|
-
path += ` > ${curr}`;
|
|
354
|
-
params = true;
|
|
347
|
+
if (!params && curr === 'state') {
|
|
348
|
+
continue;
|
|
349
|
+
} else if (!params && curr === 'routes') {
|
|
350
|
+
if (path) {
|
|
351
|
+
path += ' > ';
|
|
352
|
+
}
|
|
353
|
+
} else if (
|
|
354
|
+
!params &&
|
|
355
|
+
typeof curr === 'number' &&
|
|
356
|
+
prev === 'routes'
|
|
357
|
+
) {
|
|
358
|
+
path += pointer?.name;
|
|
359
|
+
} else if (!params) {
|
|
360
|
+
path += ` > ${curr}`;
|
|
361
|
+
params = true;
|
|
362
|
+
} else {
|
|
363
|
+
if (typeof curr === 'number' || /^[0-9]+$/.test(curr)) {
|
|
364
|
+
path += `[${curr}]`;
|
|
365
|
+
} else if (/^[a-z$_]+$/i.test(curr)) {
|
|
366
|
+
path += `.${curr}`;
|
|
355
367
|
} else {
|
|
356
|
-
|
|
357
|
-
path += `[${curr}]`;
|
|
358
|
-
} else if (/^[a-z$_]+$/i.test(curr)) {
|
|
359
|
-
path += `.${curr}`;
|
|
360
|
-
} else {
|
|
361
|
-
path += `[${JSON.stringify(curr)}]`;
|
|
362
|
-
}
|
|
368
|
+
path += `[${JSON.stringify(curr)}]`;
|
|
363
369
|
}
|
|
364
370
|
}
|
|
371
|
+
}
|
|
365
372
|
|
|
366
|
-
|
|
373
|
+
const message = `Non-serializable values were found in the navigation state. Check:\n\n${path} (${reason})\n\nThis can break usage such as persisting and restoring state. This might happen if you passed non-serializable values such as function, class instances etc. in params. If you need to use components with callbacks in your options, you can use 'navigation.setOptions' instead. See https://reactnavigation.org/docs/troubleshooting#i-get-the-warning-non-serializable-values-were-found-in-the-navigation-state for more details.`;
|
|
367
374
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
375
|
+
if (!serializableWarnings.includes(message)) {
|
|
376
|
+
serializableWarnings.push(message);
|
|
377
|
+
console.warn(message);
|
|
372
378
|
}
|
|
379
|
+
}
|
|
373
380
|
|
|
374
|
-
|
|
375
|
-
|
|
381
|
+
const duplicateRouteNamesResult =
|
|
382
|
+
checkDuplicateRouteNames(hydratedState);
|
|
376
383
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
384
|
+
if (duplicateRouteNamesResult.length) {
|
|
385
|
+
const message = `Found screens with the same name nested inside one another. Check:\n${duplicateRouteNamesResult.map(
|
|
386
|
+
(locations) => `\n${locations.join(', ')}`
|
|
387
|
+
)}\n\nThis can cause confusing behavior during navigation. Consider using unique names for each screen instead.`;
|
|
381
388
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
389
|
+
if (!duplicateNameWarnings.includes(message)) {
|
|
390
|
+
duplicateNameWarnings.push(message);
|
|
391
|
+
console.warn(message);
|
|
386
392
|
}
|
|
387
393
|
}
|
|
388
394
|
}
|
|
395
|
+
}
|
|
389
396
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
if (!isFirstMountRef.current && onStateChangeRef.current) {
|
|
393
|
-
onStateChangeRef.current(hydratedState);
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
isFirstMountRef.current = false;
|
|
397
|
-
}, [getRootState, emitter, state]);
|
|
397
|
+
emitter.emit({ type: 'state', data: { state } });
|
|
398
398
|
|
|
399
|
-
|
|
400
|
-
(
|
|
401
|
-
|
|
402
|
-
return;
|
|
403
|
-
}
|
|
399
|
+
if (!isFirstMountRef.current && onStateChangeRef.current) {
|
|
400
|
+
onStateChangeRef.current(hydratedState);
|
|
401
|
+
}
|
|
404
402
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
let message = `The action '${action.type}'${
|
|
408
|
-
payload ? ` with payload ${JSON.stringify(action.payload)}` : ''
|
|
409
|
-
} was not handled by any navigator.`;
|
|
410
|
-
|
|
411
|
-
switch (action.type) {
|
|
412
|
-
case 'PRELOAD':
|
|
413
|
-
case 'NAVIGATE':
|
|
414
|
-
case 'PUSH':
|
|
415
|
-
case 'REPLACE':
|
|
416
|
-
case 'POP_TO':
|
|
417
|
-
case 'JUMP_TO':
|
|
418
|
-
if (payload?.name) {
|
|
419
|
-
message += `\n\nDo you have a screen named '${payload.name}'?\n\nIf you're trying to navigate to a screen in a nested navigator, see https://reactnavigation.org/docs/nesting-navigators#navigating-to-a-screen-in-a-nested-navigator.\n\nIf you're using conditional rendering, navigation will happen automatically and you shouldn't navigate manually, see.`;
|
|
420
|
-
} else {
|
|
421
|
-
message += `\n\nYou need to pass the name of the screen to navigate to.\n\nSee https://reactnavigation.org/docs/navigation-actions for usage.`;
|
|
422
|
-
}
|
|
403
|
+
isFirstMountRef.current = false;
|
|
404
|
+
}, [getRootState, emitter, state]);
|
|
423
405
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
break;
|
|
430
|
-
case 'OPEN_DRAWER':
|
|
431
|
-
case 'CLOSE_DRAWER':
|
|
432
|
-
case 'TOGGLE_DRAWER':
|
|
433
|
-
message += `\n\nIs your screen inside a Drawer navigator?`;
|
|
434
|
-
break;
|
|
435
|
-
}
|
|
406
|
+
const defaultOnUnhandledAction = useLatestCallback(
|
|
407
|
+
(action: NavigationAction) => {
|
|
408
|
+
if (process.env.NODE_ENV === 'production') {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
436
411
|
|
|
437
|
-
|
|
412
|
+
const payload: Record<string, any> | undefined = action.payload;
|
|
413
|
+
|
|
414
|
+
let message = `The action '${action.type}'${
|
|
415
|
+
payload ? ` with payload ${JSON.stringify(action.payload)}` : ''
|
|
416
|
+
} was not handled by any navigator.`;
|
|
417
|
+
|
|
418
|
+
switch (action.type) {
|
|
419
|
+
case 'PRELOAD':
|
|
420
|
+
case 'NAVIGATE':
|
|
421
|
+
case 'PUSH':
|
|
422
|
+
case 'REPLACE':
|
|
423
|
+
case 'POP_TO':
|
|
424
|
+
case 'JUMP_TO':
|
|
425
|
+
if (payload?.name) {
|
|
426
|
+
message += `\n\nDo you have a screen named '${payload.name}'?\n\nIf you're trying to navigate to a screen in a nested navigator, see https://reactnavigation.org/docs/nesting-navigators#navigating-to-a-screen-in-a-nested-navigator.\n\nIf you're using conditional rendering, navigation will happen automatically and you shouldn't navigate manually, see.`;
|
|
427
|
+
} else {
|
|
428
|
+
message += `\n\nYou need to pass the name of the screen to navigate to.\n\nSee https://reactnavigation.org/docs/navigation-actions for usage.`;
|
|
429
|
+
}
|
|
438
430
|
|
|
439
|
-
|
|
431
|
+
break;
|
|
432
|
+
case 'GO_BACK':
|
|
433
|
+
case 'POP':
|
|
434
|
+
case 'POP_TO_TOP':
|
|
435
|
+
message += `\n\nIs there any screen to go back to?`;
|
|
436
|
+
break;
|
|
437
|
+
case 'OPEN_DRAWER':
|
|
438
|
+
case 'CLOSE_DRAWER':
|
|
439
|
+
case 'TOGGLE_DRAWER':
|
|
440
|
+
message += `\n\nIs your screen inside a Drawer navigator?`;
|
|
441
|
+
break;
|
|
440
442
|
}
|
|
441
|
-
);
|
|
442
443
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
444
|
+
message += `\n\nThis is a development-only warning and won't be shown in production.`;
|
|
445
|
+
|
|
446
|
+
console.error(message);
|
|
447
|
+
}
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
return (
|
|
451
|
+
<NavigationIndependentTreeContext.Provider value={false}>
|
|
452
|
+
<NavigationContainerRefContext.Provider value={navigation}>
|
|
453
|
+
<NavigationBuilderContext.Provider value={builderContext}>
|
|
454
|
+
<NavigationStateContext.Provider value={context}>
|
|
455
|
+
<UnhandledActionContext.Provider
|
|
456
|
+
value={onUnhandledAction ?? defaultOnUnhandledAction}
|
|
457
|
+
>
|
|
458
|
+
<EnsureSingleNavigator>
|
|
459
|
+
<ThemeProvider value={theme}>{children}</ThemeProvider>
|
|
460
|
+
</EnsureSingleNavigator>
|
|
461
|
+
</UnhandledActionContext.Provider>
|
|
462
|
+
</NavigationStateContext.Provider>
|
|
463
|
+
</NavigationBuilderContext.Provider>
|
|
464
|
+
</NavigationContainerRefContext.Provider>
|
|
465
|
+
</NavigationIndependentTreeContext.Provider>
|
|
466
|
+
);
|
|
467
|
+
}
|