Skip to content

Commit 795cedc

Browse files
authored
Persist navigation state on hot reloads on native (keybase#20049)
1 parent cdc0d0b commit 795cedc

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎shared/devices/device-page/container.tsx‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import DevicePage from '.'
55

66
type OwnProps = Container.RouteProps<{deviceID: string}>
77

8-
// TODO(newRouter) after committing to new router:
9-
// remove action and code that sets state.devices.selectedDeviceID.
10-
// It's a bad pattern to have navigation distributed across react-navigation
11-
// and our store. device id is purely an argument to the screen, the store
12-
// doesn't care about it.
138
export default Container.connect(
149
(state: Container.TypedState, ownProps: OwnProps) => {
1510
const id = Container.getRouteProps(ownProps, 'deviceID', '')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Hot reload management, so route state isn't lost when we hot reload
2+
module.hot && module.hot.dispose(data => (data.navState = navState))
3+
let navState: any = module.hot && module.hot.data && module.hot.data.navState
4+
5+
export const getPersistenceFunctions = () =>
6+
module.hot
7+
? {
8+
loadNavigationState: () => navState,
9+
persistNavigationState: state => (navState = state),
10+
}
11+
: {}

‎shared/router-v2/router.native.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {createSwitchNavigator, StackActions} from '@react-navigation/core'
2121
import {debounce} from 'lodash-es'
2222
import {modalRoutes, routes, loggedOutRoutes, tabRoots} from './routes'
2323
import {useScreens} from 'react-native-screens'
24+
import {getPersistenceFunctions} from './persist.native'
2425

2526
const {createStackNavigator} = Stack
2627

@@ -304,7 +305,12 @@ class RNApp extends React.PureComponent<Props> {
304305
<Kb.NativeStatusBar
305306
barStyle={Styles.isAndroid ? 'default' : this.props.isDarkMode ? 'light-content' : 'dark-content'}
306307
/>
307-
<AppContainer ref={nav => (this._nav = nav)} onNavigationStateChange={this._persistRoute} />
308+
<AppContainer
309+
ref={nav => (this._nav = nav)}
310+
onNavigationStateChange={this._persistRoute}
311+
// HMR persistence
312+
{...getPersistenceFunctions()}
313+
/>
308314
<GlobalError />
309315
<OutOfDate />
310316
<RuntimeStats />

0 commit comments

Comments
 (0)