Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions docs/with-navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sidebar_label: withNavigation

`withNavigation` is a higher order component which passes the `navigation` prop into a wrapped component. It's useful when you cannot pass the `navigation` prop into the component directly, or don't want to pass it in case of a deeply nested child.

- `withNavigation(Component)` returns a Component.
- `withNavigation(Component, config)` returns a Component.

## Example

Expand All @@ -30,10 +30,20 @@ export default withNavigation(MyBackButton);

- If you wish to use the `ref` prop on the wrapped component, you must pass the `onRef` prop instead. For example,

```
// MyBackButton.ts
```js
// MyBackButton.js
export default withNavigation(MyBackButton);

// MyNavBar.ts
// MyNavBar.js
<MyBackButton onRef={(elem) => this.backButton = elem} />
```

- If you need to use `withNavigation` HOC as the wrapped component of a custom HOC that use `onRef` prop API too, in order to prevent `onRef` callback fuction be called twice, `onRef` injectiong must be disabled using second param `config` object.

```js
// MyBackButton.js
export myCustomHOC(withNavigation(MyBackButton, { forwardRef: false }));

// MyNavBar.js
<MyBackButton onRef={(elem) => this.backButton = elem} />
```
50 changes: 50 additions & 0 deletions website/versioned_docs/version-3.x/with-navigation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
id: version-3.x-with-navigation
title: withNavigation
sidebar_label: withNavigation
original_id: with-navigation
---

`withNavigation` is a higher order component which passes the `navigation` prop into a wrapped component. It's useful when you cannot pass the `navigation` prop into the component directly, or don't want to pass it in case of a deeply nested child.

- `withNavigation(Component, config)` returns a Component.

## Example

```js
import React from 'react';
import { Button } from 'react-native';
import { withNavigation } from 'react-navigation';

class MyBackButton extends React.Component {
render() {
return <Button title="Back" onPress={() => { this.props.navigation.goBack() }} />;
}
}

// withNavigation returns a component that wraps MyBackButton and passes in the
// navigation prop
export default withNavigation(MyBackButton);
```

## Notes

- If you wish to use the `ref` prop on the wrapped component, you must pass the `onRef` prop instead. For example,

```js
// MyBackButton.js
export default withNavigation(MyBackButton);

// MyNavBar.js
<MyBackButton onRef={(elem) => this.backButton = elem} />
```

- If you need to use `withNavigation` HOC as the wrapped component of a custom HOC that use `onRef` prop API too, in order to prevent `onRef` callback fuction be called twice, `onRef` injectiong must be disabled using second param `config` object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading this over, I think this paragraph should have ### Config heading, with a clear listing of what the config object can contain (even though there is just one option available - this is a the api reference: kinda like here). Does this make sense?


```js
// MyBackButton.js
export myCustomHOC(withNavigation(MyBackButton, { forwardRef: false }));

// MyNavBar.js
<MyBackButton onRef={(elem) => this.backButton = elem} />
```
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please remove this file?