Created
January 19, 2018 14:15
-
-
Save martinemmert/70661f6b363367bc8eceb981965b997a to your computer and use it in GitHub Desktop.
This gist shows the a working type definition for navigationOptions for your screens when using Typescript with react and react-navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
import { Text, View } from "react-native"; | |
import { NavigationScreenConfigProps, NavigationScreenProps } from "react-navigation"; | |
type Params = { title: string }; | |
type DemoScreenProps = NavigationScreenConfigProps; | |
class DemoScreen extends React.PureComponent<DemoScreenProps> { | |
public static navigationOptions = ({ navigation }: NavigationScreenProps<Params>) => ({ | |
title: navigation.state.params.title, | |
}); | |
public render() { | |
return ( | |
<View> | |
<Text>{`Hello from ${this.props.navigation.state.params.title}`}</Text> | |
</View> | |
); | |
} | |
} | |
export default DemoScreen; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with your code I'm getting
TS2532: Object is possibly 'undefined'
onand also I get autocompletion for
.state
and then for.params
inside it, but not for.title
...Update:
Actually, it almost works, it's just you need to make sure
navigation.state.params
is defined first - see the complete explanation in this StackOverflow answer.