Skip to content

Commit a9533c2

Browse files
authored
Merge pull request react-navigation#52 from peterpme/add-get-param-docs
getParam docs
2 parents 4eae99a + 33560c2 commit a9533c2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

docs/navigation-prop.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Each `screen` component in your app is provided with the `navigation` prop autom
1212
* `addListener` - subscribe to updates to navigation lifecycle
1313
* `state` - current state/routes
1414
* `setParams` - make changes to route's params
15+
* `getParam` - get a specific param with fallback
1516
* `dispatch` - send an action to router
1617

1718
It's important to highlight the `navigation` prop is _not_ passed in to _all_ components; only `screen` components receive this prop automatically! React Navigation doesn't do anything magic here. For example, if you were to define a `MyBackButton` component and render it as a child of a screen component, you would not be able to access the `navigation` prop on it.
@@ -177,6 +178,24 @@ class ProfileScreen extends React.Component {
177178
}
178179
```
179180

181+
### `getParam` - Get a specific param value with a fallback
182+
183+
In the past, you may have encountered the frightful scenario of accessing a `param` when `params` is undefined. Instead of accessing the param directly, you can call `getParam` instead.
184+
185+
Before:
186+
```js
187+
const { name } = this.props.navigation.state.params
188+
```
189+
190+
if `params` is `undefined`, this fails
191+
192+
After:
193+
```js
194+
const name = this.props.navigation.getParam('name', 'Peter')
195+
```
196+
197+
if `name` or `param` are undefined, set the fallback to `Peter`.
198+
180199
## Stack Actions
181200

182201
The following actions will work within any StackNavigator:

0 commit comments

Comments
 (0)