Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
feat: add native container
Browse files Browse the repository at this point in the history
  • Loading branch information
osdnk authored and satya164 committed Aug 24, 2019
1 parent 1bbd6ac commit d26b77f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
9 changes: 2 additions & 7 deletions packages/core/src/NavigationContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ import {
PartialState,
NavigationAction,
NavigationContainerRef,
NavigationContainerProps,
} from './types';

type State = NavigationState | PartialState<NavigationState> | 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'?";

Expand Down Expand Up @@ -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<NavigationContainerRef>
) {
const [state, setNavigationState] = React.useState<State>(() =>
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/types.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as BaseActions from './BaseActions';
import * as React from 'react';

export type CommonAction = BaseActions.Action;

Expand Down Expand Up @@ -344,6 +345,14 @@ export type NavigationHelpers<
): void;
};

export type NavigationContainerProps = {
initialState?: InitialState;
onStateChange?: (
state: NavigationState | PartialState<NavigationState> | undefined
) => void;
children: React.ReactNode;
};

export type NavigationProp<
ParamList extends ParamListBase,
RouteName extends keyof ParamList = string,
Expand Down
9 changes: 3 additions & 6 deletions packages/example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -60,8 +59,6 @@ Asset.loadAsync(StackAssets);
export default function App() {
const containerRef = React.useRef<NavigationContainerRef>();

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
Expand Down Expand Up @@ -116,7 +113,7 @@ export default function App() {
}

return (
<NavigationContainer
<NativeContainer
ref={containerRef}
initialState={initialState}
onStateChange={state =>
Expand Down Expand Up @@ -175,6 +172,6 @@ export default function App() {
)}
</Drawer.Screen>
</Drawer.Navigator>
</NavigationContainer>
</NativeContainer>
);
}
38 changes: 38 additions & 0 deletions packages/native/src/NativeContainer.tsx
Original file line number Diff line number Diff line change
@@ -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<NavigationContainerRef>
) {
const refContainer = React.useRef<NavigationContainerRef>(null);

useBackButton(refContainer);
React.useImperativeHandle(ref, () => refContainer.current);

return (
<NavigationContainer
{...props}
ref={refContainer}
children={props.children}
/>
);
});

export default NativeContainer;
1 change: 1 addition & 0 deletions packages/native/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as useBackButton } from './useBackButton';
export { default as useLinking } from './useLinking';
export { default as NativeContainer } from './NativeContainer';

0 comments on commit d26b77f

Please sign in to comment.