-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit makes static options added to components strongly typed. I also updated a few screens in the Playground app to reflect this, and also corrected rightButtons and leftButtons option which accepts both single button and an array of buttons.
- Loading branch information
Showing
15 changed files
with
185 additions
and
75 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
--- | ||
id: third-party-typescript | ||
title: TypeScript | ||
sidebar_label: TypeScript | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
## Strongly typed components | ||
|
||
Both functional and class components can be typed to get the benefits of strongly typed options and props. | ||
|
||
<Tabs | ||
defaultValue="clazz" | ||
values={[ | ||
{ label: 'Class Component', value: 'clazz' }, | ||
{ label: 'Functional Component', value: 'functional' }, | ||
]} | ||
> | ||
|
||
<TabItem value="clazz"> | ||
|
||
```tsx | ||
import { NavigationComponent } from 'react-native-navigation'; | ||
|
||
interface Props extends NavigationComponentProps { | ||
age: number | ||
} | ||
|
||
export default class MyComponent extends NavigationComponent<Props> { | ||
// Options are strongly typed | ||
static options() { | ||
return { | ||
statusBar: { | ||
// Some options are of union type. We're using `as const` to let the- | ||
// TS compiler know this value is not a regular string | ||
style: 'dark' as const, | ||
}, | ||
topBar: { | ||
title: { | ||
text: 'My Screen', | ||
} | ||
}; | ||
} | ||
} | ||
|
||
constructor(props: Props) { | ||
super(props); | ||
} | ||
} | ||
``` | ||
|
||
</TabItem> | ||
|
||
<TabItem value="functional"> | ||
|
||
```tsx | ||
import { NavigationFunctionComponent } from 'react-native-navigation'; | ||
|
||
interface Props { | ||
name: string; | ||
} | ||
|
||
const MyFunctionalComponent: NavigationFunctionComponent<Props> = ({ componentId, name }) => { | ||
return <View />; | ||
}; | ||
|
||
// Static options are also supported! | ||
MyFunctionalComponent.options = { | ||
topBar: { | ||
title: { | ||
text: 'Hello functional component', | ||
}, | ||
}, | ||
}; | ||
export default MyFunctionalComponent; | ||
``` | ||
|
||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
## Typed props in commands | ||
|
||
Arguments are passed to components view the `passProp`. This is a common source for errors as these props tend to change overtime. Luckily we can type the passProps property to avoid these regressions. The example below shows how to declare types for passProps when pushing a screen. | ||
|
||
```tsx | ||
class Props: { | ||
name: string | ||
} | ||
|
||
Navigation.push<Props>(componentId, { | ||
component: { | ||
name: 'MyComponent', | ||
passProps: { | ||
name: 'Bob', | ||
color: 'red', // Compilation error! color isn't declared in Props | ||
} | ||
} | ||
}); | ||
``` | ||
|
||
The following commands accept typed passProps: | ||
|
||
- push | ||
- setStackRoot | ||
- showModal | ||
- showOverlay |
Oops, something went wrong.