UNPKG

2.74 kBJavaScriptView Raw
1import * as React from 'react';
2import EnsureSingleNavigator from './EnsureSingleNavigator';
3import NavigationStateContext from './NavigationStateContext';
4import StaticContainer from './StaticContainer';
5import useOptionsGetters from './useOptionsGetters';
6
7/**
8 * Component which takes care of rendering the screen for a route.
9 * It provides all required contexts and applies optimizations when applicable.
10 */
11export default function SceneView(_ref) {
12 let {
13 screen,
14 route,
15 navigation,
16 routeState,
17 getState,
18 setState,
19 options,
20 clearOptions
21 } = _ref;
22 const navigatorKeyRef = React.useRef();
23 const getKey = React.useCallback(() => navigatorKeyRef.current, []);
24 const {
25 addOptionsGetter
26 } = useOptionsGetters({
27 key: route.key,
28 options,
29 navigation
30 });
31 const setKey = React.useCallback(key => {
32 navigatorKeyRef.current = key;
33 }, []);
34 const getCurrentState = React.useCallback(() => {
35 const state = getState();
36 const currentRoute = state.routes.find(r => r.key === route.key);
37 return currentRoute ? currentRoute.state : undefined;
38 }, [getState, route.key]);
39 const setCurrentState = React.useCallback(child => {
40 const state = getState();
41 setState({ ...state,
42 routes: state.routes.map(r => r.key === route.key ? { ...r,
43 state: child
44 } : r)
45 });
46 }, [getState, route.key, setState]);
47 const isInitialRef = React.useRef(true);
48 React.useEffect(() => {
49 isInitialRef.current = false;
50 }); // Clear options set by this screen when it is unmounted
51
52 React.useEffect(() => {
53 return clearOptions; // eslint-disable-next-line react-hooks/exhaustive-deps
54 }, []);
55 const getIsInitial = React.useCallback(() => isInitialRef.current, []);
56 const context = React.useMemo(() => ({
57 state: routeState,
58 getState: getCurrentState,
59 setState: setCurrentState,
60 getKey,
61 setKey,
62 getIsInitial,
63 addOptionsGetter
64 }), [routeState, getCurrentState, setCurrentState, getKey, setKey, getIsInitial, addOptionsGetter]);
65 const ScreenComponent = screen.getComponent ? screen.getComponent() : screen.component;
66 return /*#__PURE__*/React.createElement(NavigationStateContext.Provider, {
67 value: context
68 }, /*#__PURE__*/React.createElement(EnsureSingleNavigator, null, /*#__PURE__*/React.createElement(StaticContainer, {
69 name: screen.name,
70 render: ScreenComponent || screen.children,
71 navigation: navigation,
72 route: route
73 }, ScreenComponent !== undefined ? /*#__PURE__*/React.createElement(ScreenComponent, {
74 navigation: navigation,
75 route: route
76 }) : screen.children !== undefined ? screen.children({
77 navigation,
78 route
79 }) : null)));
80}
81//# sourceMappingURL=SceneView.js.map
\No newline at end of file