UNPKG

933 BTypeScriptView Raw
1import type { NavigationState, ParamListBase } from '@react-navigation/routers';
2import * as React from 'react';
3
4import CurrentRenderContext from './CurrentRenderContext';
5import type {
6 Descriptor,
7 NavigationHelpers,
8 NavigationProp,
9 RouteProp,
10} from './types';
11
12type Options = {
13 state: NavigationState;
14 navigation: NavigationHelpers<ParamListBase>;
15 descriptors: Record<
16 string,
17 Descriptor<object, NavigationProp<ParamListBase>, RouteProp<ParamListBase>>
18 >;
19};
20
21/**
22 * Write the current options, so that server renderer can get current values
23 * Mutating values like this is not safe in async mode, but it doesn't apply to SSR
24 */
25export default function useCurrentRender({
26 state,
27 navigation,
28 descriptors,
29}: Options) {
30 const current = React.useContext(CurrentRenderContext);
31
32 if (current && navigation.isFocused()) {
33 current.options = descriptors[state.routes[state.index].key].options;
34 }
35}