This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
generated from satya164/typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a getRootState method (#119)
- Loading branch information
Showing
8 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as React from 'react'; | ||
import NavigationBuilderContext from './NavigationBuilderContext'; | ||
import { NavigationState } from './types'; | ||
import NavigationRouteContext from './NavigationRouteContext'; | ||
|
||
export default function useOnGetState({ | ||
getStateForRoute, | ||
getState, | ||
}: { | ||
getStateForRoute: (routeName: string) => NavigationState | undefined; | ||
getState: () => NavigationState; | ||
}) { | ||
const { addStateGetter } = React.useContext(NavigationBuilderContext); | ||
const route = React.useContext(NavigationRouteContext); | ||
const key = route ? route.key : 'root'; | ||
|
||
const getter = React.useCallback(() => { | ||
const state = getState(); | ||
return { | ||
...state, | ||
routes: state.routes.map(route => ({ | ||
...route, | ||
state: getStateForRoute(route.key), | ||
})), | ||
}; | ||
}, [getState, getStateForRoute]); | ||
|
||
React.useEffect(() => { | ||
return addStateGetter && addStateGetter(key, getter); | ||
}, [addStateGetter, getter, key]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as React from 'react'; | ||
import { NavigatorStateGetter } from './NavigationBuilderContext'; | ||
|
||
/** | ||
* Hook which lets child navigators add getters to be called for obtaining rehydrated state. | ||
*/ | ||
|
||
export default function useStateGetters() { | ||
const stateGetters = React.useRef<Record<string, NavigatorStateGetter>>({}); | ||
|
||
const getStateForRoute = React.useCallback( | ||
routeKey => | ||
stateGetters.current[routeKey] === undefined | ||
? undefined | ||
: stateGetters.current[routeKey](), | ||
[stateGetters] | ||
); | ||
|
||
const addStateGetter = React.useCallback( | ||
(key: string, getter: NavigatorStateGetter) => { | ||
stateGetters.current[key] = getter; | ||
|
||
return () => { | ||
// @ts-ignore | ||
stateGetters.current[key] = undefined; | ||
}; | ||
}, | ||
[] | ||
); | ||
|
||
return { | ||
getStateForRoute, | ||
addStateGetter, | ||
}; | ||
} |