Skip to content

Commit

Permalink
fix(components): Allow access to the inner reference of viewport-awar…
Browse files Browse the repository at this point in the history
…e components (#121)

* Allow access to the inner reference of viewport-aware components

* Add sample usage of innerRef
  • Loading branch information
Blagoja Evkoski committed Sep 30, 2018
1 parent 01b6804 commit 4f88275
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ render() {
source={{ uri: 'https://facebook.github.io/react-native/img/header_logo.png' }}
preTriggerRatio={0.5}
onViewportEnter={() => console.log('Entered!')}
onViewportLeave={() => console.log('Left!')} />
onViewportLeave={() => console.log('Left!')}
innerRef={ref => (this._ref = ref)} />
)
}
```
Expand All @@ -58,6 +59,7 @@ render() {
|**`preTriggerRatio`**| Determines pre-triggering of `inViewport`. Useful for rendering components beforehand to improve user experience. A ratio of `0.5` means that the effective viewport will be twice the size of the real viewport. | `0` |
|**`onViewportEnter`**| Invoked when the component enters the viewport. | `null` |
|**`onViewportLeave`**| Invoked when the component leaves the viewport. | `null` |
|**`innerRef`**| Allows access to the reference of the wrapped component. | `null` |

### With Placeholder

Expand Down
6 changes: 5 additions & 1 deletion packages/components/src/viewport/aware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ export default WrappedComponent => {
render() {
return (
<WrappedComponent
ref={ref => (this.nodeHandle = findNodeHandle(ref))}
{...this.props}
inViewport={this.state.inViewport}
ref={ref => {
this.nodeHandle = findNodeHandle(ref)
this.props.innerRef && this.props.innerRef(ref)
}}
/>
)
}
Expand All @@ -99,6 +102,7 @@ export default WrappedComponent => {
preTriggerRatio: PropTypes.number,
onViewportEnter: PropTypes.func,
onViewportLeave: PropTypes.func,
innerRef: PropTypes.func,
}

static contextTypes = {
Expand Down

0 comments on commit 4f88275

Please sign in to comment.