@react-navigation/core 7.0.4 → 7.0.6

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.
@@ -21,6 +21,7 @@ type Options<State extends NavigationState, Action extends NavigationAction> = {
21
21
  getState: () => State;
22
22
  emitter: NavigationEventEmitter<any>;
23
23
  router: Router<State, Action>;
24
+ stateRef: React.RefObject<State | null>;
24
25
  };
25
26
 
26
27
  /**
@@ -38,6 +39,7 @@ export function useNavigationHelpers<
38
39
  getState,
39
40
  emitter,
40
41
  router,
42
+ stateRef,
41
43
  }: Options<State, Action>) {
42
44
  const onUnhandledAction = React.useContext(UnhandledActionContext);
43
45
  const parentNavigationHelpers = React.useContext(NavigationContext);
@@ -99,17 +101,29 @@ export function useNavigationHelpers<
99
101
 
100
102
  return parentNavigationHelpers;
101
103
  },
102
- getState,
104
+ getState: (): State => {
105
+ // FIXME: Workaround for when the state is read during render
106
+ // By this time, we haven't committed the new state yet
107
+ // Without this `useSyncExternalStore` will keep reading the old state
108
+ // This may result in `useNavigationState` or `useIsFocused` returning wrong values
109
+ // Apart from `useSyncExternalStore`, `getState` should never be called during render
110
+ if (stateRef.current != null) {
111
+ return stateRef.current;
112
+ }
113
+
114
+ return getState();
115
+ },
103
116
  } as NavigationHelpers<ParamListBase, EventMap> & ActionHelpers;
104
117
 
105
118
  return navigationHelpers;
106
119
  }, [
107
- navigatorId,
120
+ router,
121
+ parentNavigationHelpers,
108
122
  emitter.emit,
109
123
  getState,
110
124
  onAction,
111
125
  onUnhandledAction,
112
- parentNavigationHelpers,
113
- router,
126
+ navigatorId,
127
+ stateRef,
114
128
  ]);
115
129
  }