Skip to content

Instantly share code, notes, and snippets.

@martinemmert
Created January 19, 2018 14:15
Show Gist options
  • Save martinemmert/70661f6b363367bc8eceb981965b997a to your computer and use it in GitHub Desktop.
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
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;
@miguelrs
Copy link

miguelrs commented May 11, 2018

with your code I'm getting TS2532: Object is possibly 'undefined' on

navigation.state.params.title

and 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment