diff --git a/packages/core/src/NavigationContainer.tsx b/packages/core/src/NavigationContainer.tsx index 18300377..ef3852e5 100644 --- a/packages/core/src/NavigationContainer.tsx +++ b/packages/core/src/NavigationContainer.tsx @@ -12,16 +12,11 @@ import { PartialState, NavigationAction, NavigationContainerRef, + NavigationContainerProps, } from './types'; type State = NavigationState | PartialState | undefined; -type Props = { - initialState?: InitialState; - onStateChange?: (state: State) => void; - children: React.ReactNode; -}; - const MISSING_CONTEXT_ERROR = "We couldn't find a navigation context. Have you wrapped your app with 'NavigationContainer'?"; @@ -84,7 +79,7 @@ const getPartialState = ( * @param props.ref Ref object which refers to the navigation object containing helper methods. */ const Container = React.forwardRef(function NavigationContainer( - { initialState, onStateChange, children }: Props, + { initialState, onStateChange, children }: NavigationContainerProps, ref: React.Ref ) { const [state, setNavigationState] = React.useState(() => diff --git a/packages/core/src/types.tsx b/packages/core/src/types.tsx index 09036127..52c6f812 100644 --- a/packages/core/src/types.tsx +++ b/packages/core/src/types.tsx @@ -1,4 +1,5 @@ import * as BaseActions from './BaseActions'; +import * as React from 'react'; export type CommonAction = BaseActions.Action; @@ -344,6 +345,14 @@ export type NavigationHelpers< ): void; }; +export type NavigationContainerProps = { + initialState?: InitialState; + onStateChange?: ( + state: NavigationState | PartialState | undefined + ) => void; + children: React.ReactNode; +}; + export type NavigationProp< ParamList extends ParamListBase, RouteName extends keyof ParamList = string, diff --git a/packages/example/src/index.tsx b/packages/example/src/index.tsx index 75ae016b..fcbf1481 100644 --- a/packages/example/src/index.tsx +++ b/packages/example/src/index.tsx @@ -4,12 +4,11 @@ import { Linking } from 'expo'; import { Appbar, List } from 'react-native-paper'; import { Asset } from 'expo-asset'; import { - NavigationContainer, InitialState, getStateFromPath, NavigationContainerRef, } from '@react-navigation/core'; -import { useBackButton, useLinking } from '@react-navigation/native'; +import { useLinking, NativeContainer } from '@react-navigation/native'; import { createDrawerNavigator, DrawerNavigationProp, @@ -60,8 +59,6 @@ Asset.loadAsync(StackAssets); export default function App() { const containerRef = React.useRef(); - useBackButton(containerRef); - // To test deep linking on, run the following in the Terminal: // Android: adb shell am start -a android.intent.action.VIEW -d "exp://127.0.0.1:19000/--/simple-stack" // iOS: xcrun simctl openurl booted exp://127.0.0.1:19000/--/simple-stack @@ -116,7 +113,7 @@ export default function App() { } return ( - @@ -175,6 +172,6 @@ export default function App() { )} - + ); } diff --git a/packages/native/src/NativeContainer.tsx b/packages/native/src/NativeContainer.tsx new file mode 100644 index 00000000..5136cf74 --- /dev/null +++ b/packages/native/src/NativeContainer.tsx @@ -0,0 +1,38 @@ +import * as React from 'react'; +import { + NavigationContainer, + NavigationContainerProps, + NavigationContainerRef, +} from '@react-navigation/core'; +import useBackButton from './useBackButton'; + +/** + * Container component which holds the navigation state + * designed for mobile apps. + * This should be rendered at the root wrapping the whole app. + * + * @param props.initialState Initial state object for the navigation tree. + * @param props.onStateChange Callback which is called with the latest navigation state when it changes. + * @param props.children Child elements to render the content. + * @param props.ref Ref object which refers to the navigation object containing helper methods. + */ + +const NativeContainer = React.forwardRef(function NativeContainer( + props: NavigationContainerProps, + ref: React.Ref +) { + const refContainer = React.useRef(null); + + useBackButton(refContainer); + React.useImperativeHandle(ref, () => refContainer.current); + + return ( + + ); +}); + +export default NativeContainer; diff --git a/packages/native/src/index.tsx b/packages/native/src/index.tsx index a1ef1fcc..9de4c02e 100644 --- a/packages/native/src/index.tsx +++ b/packages/native/src/index.tsx @@ -1,2 +1,3 @@ export { default as useBackButton } from './useBackButton'; export { default as useLinking } from './useLinking'; +export { default as NativeContainer } from './NativeContainer';