Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

Commit e61f594

Browse files
committed
feat: add a method to reset root navigator state
1 parent 8b78d61 commit e61f594

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

packages/core/src/NavigationContainer.tsx

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import * as CommonActions from './CommonActions';
33
import EnsureSingleNavigator from './EnsureSingleNavigator';
44
import NavigationBuilderContext from './NavigationBuilderContext';
5+
import ResetRootContext from './ResetRootContext';
56
import useFocusedListeners from './useFocusedListeners';
67
import useDevTools from './useDevTools';
78

@@ -86,6 +87,23 @@ const Container = React.forwardRef(function NavigationContainer(
8687
getPartialState(initialState)
8788
);
8889

90+
const navigationStateRef = React.useRef<State>();
91+
const transactionStateRef = React.useRef<State | null>(null);
92+
const isTransactionActiveRef = React.useRef<boolean>(false);
93+
const isFirstMountRef = React.useRef<boolean>(true);
94+
const skipTrackingRef = React.useRef<boolean>(false);
95+
96+
const reset = React.useCallback((state: NavigationState) => {
97+
skipTrackingRef.current = true;
98+
setNavigationState(state);
99+
}, []);
100+
101+
const { trackState, trackAction } = useDevTools({
102+
name: '@react-navigation',
103+
reset,
104+
state,
105+
});
106+
89107
const { listeners, addListener: addFocusedListener } = useFocusedListeners();
90108

91109
const dispatch = (
@@ -106,6 +124,14 @@ const Container = React.forwardRef(function NavigationContainer(
106124
}
107125
};
108126

127+
const resetRoot = React.useCallback(
128+
(state: PartialState<NavigationState> | NavigationState) => {
129+
trackAction('@@RESET_ROOT');
130+
setNavigationState(state);
131+
},
132+
[trackAction]
133+
);
134+
109135
React.useImperativeHandle(ref, () => ({
110136
...(Object.keys(CommonActions) as Array<keyof typeof CommonActions>).reduce<
111137
any
@@ -120,31 +146,11 @@ const Container = React.forwardRef(function NavigationContainer(
120146
);
121147
return acc;
122148
}, {}),
123-
resetRoot: (state: PartialState<NavigationState> | NavigationState) => {
124-
trackAction('@@RESET_ROOT');
125-
setNavigationState(state);
126-
},
149+
resetRoot,
127150
dispatch,
128151
canGoBack,
129152
}));
130153

131-
const navigationStateRef = React.useRef<State>();
132-
const transactionStateRef = React.useRef<State | null>(null);
133-
const isTransactionActiveRef = React.useRef<boolean>(false);
134-
const isFirstMountRef = React.useRef<boolean>(true);
135-
const skipTrackingRef = React.useRef<boolean>(false);
136-
137-
const reset = React.useCallback((state: NavigationState) => {
138-
skipTrackingRef.current = true;
139-
setNavigationState(state);
140-
}, []);
141-
142-
const { trackState, trackAction } = useDevTools({
143-
name: '@react-navigation',
144-
reset,
145-
state,
146-
});
147-
148154
const builderContext = React.useMemo(
149155
() => ({
150156
addFocusedListener,
@@ -220,7 +226,9 @@ const Container = React.forwardRef(function NavigationContainer(
220226
return (
221227
<NavigationBuilderContext.Provider value={builderContext}>
222228
<NavigationStateContext.Provider value={context}>
223-
<EnsureSingleNavigator>{children}</EnsureSingleNavigator>
229+
<ResetRootContext.Provider value={resetRoot}>
230+
<EnsureSingleNavigator>{children}</EnsureSingleNavigator>
231+
</ResetRootContext.Provider>
224232
</NavigationStateContext.Provider>
225233
</NavigationBuilderContext.Provider>
226234
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from 'react';
2+
import { NavigationState, PartialState } from './types';
3+
4+
/**
5+
* Context which holds the method to reset root navigator state.
6+
*/
7+
const ResetRootContext = React.createContext<
8+
(state: PartialState<NavigationState> | NavigationState) => void
9+
>(() => {
10+
throw new Error(
11+
"We couldn't find a way to reset root state. Have you wrapped your app with 'NavigationContainer'?"
12+
);
13+
});
14+
15+
export default ResetRootContext;

packages/core/src/types.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,13 @@ type NavigationHelpersCommon<
316316
*/
317317
reset(state: PartialState<State> | State): void;
318318

319+
/**
320+
* Reset the navigation state of the root navigator to the provided state.
321+
*
322+
* @param state Navigation state object.
323+
*/
324+
resetRoot(state: PartialState<NavigationState> | NavigationState): void;
325+
319326
/**
320327
* Go back to the previous route in history.
321328
*/

packages/core/src/useNavigationHelpers.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import * as CommonActions from './CommonActions';
33
import NavigationContext from './NavigationContext';
4+
import ResetRootContext from './ResetRootContext';
45
import { NavigationStateContext } from './NavigationContainer';
56
import { NavigationEventEmitter } from './useEventEmitter';
67
import {
@@ -36,6 +37,7 @@ export default function useNavigationHelpers<
3637
Action extends NavigationAction,
3738
EventMap extends { [key: string]: any }
3839
>({ onAction, getState, emitter, router }: Options<State, Action>) {
40+
const resetRoot = React.useContext(ResetRootContext);
3941
const parentNavigationHelpers = React.useContext(NavigationContext);
4042
const { performTransaction } = React.useContext(NavigationStateContext);
4143

@@ -65,6 +67,7 @@ export default function useNavigationHelpers<
6567
return {
6668
...parentNavigationHelpers,
6769
...helpers,
70+
resetRoot,
6871
dispatch,
6972
emit: emitter.emit,
7073
isFocused: parentNavigationHelpers
@@ -83,6 +86,7 @@ export default function useNavigationHelpers<
8386
router,
8487
getState,
8588
parentNavigationHelpers,
89+
resetRoot,
8690
emitter.emit,
8791
performTransaction,
8892
onAction,

0 commit comments

Comments
 (0)