@react-navigation/core 8.0.0-alpha.1 → 8.0.0-alpha.3

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.
@@ -17,7 +17,7 @@ type Options<
17
17
  ScreenOptions extends {},
18
18
  EventMap extends Record<string, any>,
19
19
  > = {
20
- state: State;
20
+ routes: State['routes'];
21
21
  getState: () => State;
22
22
  navigation: NavigationHelpers<ParamListBase> &
23
23
  Partial<NavigationProp<ParamListBase, string, any, any, any>>;
@@ -65,7 +65,7 @@ export function useNavigationCache<
65
65
  EventMap extends Record<string, any>,
66
66
  ActionHelpers extends Record<string, (...args: any) => void>,
67
67
  >({
68
- state,
68
+ routes,
69
69
  getState,
70
70
  navigation,
71
71
  setOptions,
@@ -75,67 +75,6 @@ export function useNavigationCache<
75
75
  const parentNavigationHelpers = React.useContext(NavigationContext);
76
76
  const { stackRef } = React.useContext(NavigationBuilderContext);
77
77
 
78
- const base = React.useMemo((): NavigationItem<
79
- State,
80
- ScreenOptions,
81
- EventMap,
82
- ActionHelpers
83
- > &
84
- ActionHelpers => {
85
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
86
- const { emit, ...rest } = navigation;
87
-
88
- const actions = {
89
- ...router.actionCreators,
90
- ...CommonActions,
91
- };
92
-
93
- const dispatch = () => {
94
- throw new Error(
95
- 'Actions cannot be dispatched from a placeholder screen.'
96
- );
97
- };
98
-
99
- const helpers = Object.keys(actions).reduce<Record<string, () => void>>(
100
- (acc, name) => {
101
- acc[name] = dispatch;
102
-
103
- return acc;
104
- },
105
- {}
106
- ) as ActionHelpers;
107
-
108
- // @ts-expect-error: type of getParent does not match
109
- return {
110
- ...rest,
111
- ...helpers,
112
- addListener: () => {
113
- // Event listeners are not supported for placeholder screens
114
-
115
- return () => {
116
- // Empty function
117
- };
118
- },
119
- removeListener: () => {
120
- // Event listeners are not supported for placeholder screens
121
- },
122
- dispatch,
123
- getParent: (routeName) => {
124
- if (routeName !== undefined) {
125
- throw new Error(
126
- 'Getting parent by route name is not supported from a placeholder screen.'
127
- );
128
- }
129
-
130
- return parentNavigationHelpers;
131
- },
132
- setOptions: () => {
133
- throw new Error('Options cannot be set from a placeholder screen.');
134
- },
135
- isFocused: () => false,
136
- };
137
- }, [parentNavigationHelpers, navigation, router.actionCreators]);
138
-
139
78
  // Cache object which holds navigation objects for each screen
140
79
  // We use `React.useMemo` instead of `React.useRef` coz we want to invalidate it when deps change
141
80
  // In reality, these deps will rarely change, if ever
@@ -149,10 +88,10 @@ export function useNavigationCache<
149
88
  >,
150
89
  }),
151
90
  // eslint-disable-next-line react-hooks/exhaustive-deps
152
- [base, getState, parentNavigationHelpers, navigation, setOptions, emitter]
91
+ [getState, parentNavigationHelpers, navigation, setOptions, emitter]
153
92
  );
154
93
 
155
- cache.current = state.routes.reduce<
94
+ cache.current = routes.reduce<
156
95
  NavigationCache<State, ScreenOptions, EventMap, ActionHelpers>
157
96
  >((acc, route) => {
158
97
  const previous = cache.current[route.key];
@@ -213,8 +152,11 @@ export function useNavigationCache<
213
152
  {}
214
153
  );
215
154
 
155
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
156
+ const { emit, ...rest } = navigation;
157
+
216
158
  acc[route.key] = {
217
- ...base,
159
+ ...rest,
218
160
  ...helpers,
219
161
  // FIXME: too much work to fix the types for now
220
162
  ...(emitter.create(route.key) as any),
@@ -239,7 +181,7 @@ export function useNavigationCache<
239
181
  }));
240
182
  },
241
183
  isFocused: () => {
242
- const state = base.getState();
184
+ const state = rest.getState();
243
185
 
244
186
  if (state.routes[state.index].key !== route.key) {
245
187
  return false;
@@ -255,8 +197,5 @@ export function useNavigationCache<
255
197
  return acc;
256
198
  }, {});
257
199
 
258
- return {
259
- base,
260
- navigations: cache.current,
261
- };
200
+ return cache.current;
262
201
  }