UNPKG

944 BJavaScriptView Raw
1import * as React from 'react';
2import NavigationBuilderContext from './NavigationBuilderContext';
3
4/**
5 * Hook to handle focus actions for a route.
6 * Focus action needs to be treated specially, coz when a nested route is focused,
7 * the parent navigators also needs to be focused.
8 */
9export default function useOnRouteFocus(_ref) {
10 let {
11 router,
12 getState,
13 key: sourceRouteKey,
14 setState
15 } = _ref;
16 const {
17 onRouteFocus: onRouteFocusParent
18 } = React.useContext(NavigationBuilderContext);
19 return React.useCallback(key => {
20 const state = getState();
21 const result = router.getStateForRouteFocus(state, key);
22
23 if (result !== state) {
24 setState(result);
25 }
26
27 if (onRouteFocusParent !== undefined && sourceRouteKey !== undefined) {
28 onRouteFocusParent(sourceRouteKey);
29 }
30 }, [getState, onRouteFocusParent, router, setState, sourceRouteKey]);
31}
32//# sourceMappingURL=useOnRouteFocus.js.map
\No newline at end of file