From d3b08f27ebef824a5f9bf9481d497b6d34660c5e Mon Sep 17 00:00:00 2001 From: Ashoat Tevosyan Date: Wed, 22 Nov 2017 06:42:17 -0800 Subject: [PATCH] Add NavigationContainer and NavigationContainerProps types (#3030) Adding types for these fixes the errors we're seeing when using `TabNavigator` and `StackNavigator`. --- .../react-navigation/src/TypeDefinition.js | 29 +++++++++++++++++-- .../src/createNavigationContainer.js | 28 +++++++----------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/packages/react-navigation/src/TypeDefinition.js b/packages/react-navigation/src/TypeDefinition.js index 32de0270..c6f811c2 100644 --- a/packages/react-navigation/src/TypeDefinition.js +++ b/packages/react-navigation/src/TypeDefinition.js @@ -228,9 +228,9 @@ export type NavigationScreenDetails = { navigation: NavigationScreenProp, }; -export type NavigationScreenOptions = {| +export type NavigationScreenOptions = { title?: string, -|}; +}; export type NavigationScreenConfigProps = { navigation: NavigationScreenProp, @@ -421,6 +421,31 @@ export type NavigationNavigatorProps = { navigationOptions?: O, }; +/** + * Navigation container + */ + +export type NavigationContainer< + State: NavigationState, + Options: {}, + Props: {} +> = React.ComponentType & Props> & { + router: NavigationRouter, + navigationOptions?: ?NavigationScreenConfig, +}; + +export type NavigationContainerProps = { + uriPrefix?: string | RegExp, + onNavigationStateChange?: ( + NavigationState, + NavigationState, + NavigationAction + ) => void, + navigation?: NavigationScreenProp, + screenProps?: *, + navigationOptions?: O, +}; + /** * Gestures, Animations, and Interpolators */ diff --git a/packages/react-navigation/src/createNavigationContainer.js b/packages/react-navigation/src/createNavigationContainer.js index 6709f088..464c3771 100644 --- a/packages/react-navigation/src/createNavigationContainer.js +++ b/packages/react-navigation/src/createNavigationContainer.js @@ -13,20 +13,10 @@ import type { NavigationNavigator, PossiblyDeprecatedNavigationAction, NavigationInitAction, + NavigationContainerProps, + NavigationContainer, } from './TypeDefinition'; -type Props = { - uriPrefix?: string | RegExp, - onNavigationStateChange?: ( - NavigationState, - NavigationState, - NavigationAction - ) => void, - navigation?: NavigationScreenProp, - screenProps?: *, - navigationOptions?: O, -}; - type State = { nav: ?NavState, }; @@ -40,15 +30,19 @@ type State = { export default function createNavigationContainer( // Let the NavigationNavigator props flowwwww Component: NavigationNavigator -) { - class NavigationContainer extends React.Component, State> { +): NavigationContainer { + class NavigationContainer extends React.Component< + NavigationContainerProps, + State + > { subs: ?{ remove: () => void, } = null; static router = Component.router; + static navigationOptions = null; - constructor(props: Props) { + constructor(props: NavigationContainerProps) { super(props); this._validateProps(props); @@ -64,7 +58,7 @@ export default function createNavigationContainer( return !this.props.navigation; } - _validateProps(props: Props) { + _validateProps(props: NavigationContainerProps) { if (this._isStateful()) { return; } @@ -142,7 +136,7 @@ export default function createNavigationContainer( } } - componentWillReceiveProps(nextProps: Props) { + componentWillReceiveProps(nextProps: NavigationContainerProps) { this._validateProps(nextProps); }