Skip to content
This repository has been archived by the owner on Feb 15, 2019. It is now read-only.

Commit

Permalink
withNavigation on stateless components does not work. add withRef option
Browse files Browse the repository at this point in the history
Closes #447

fbshipit-source-id: f696928
  • Loading branch information
chirag04 authored and expbot committed Apr 18, 2017
1 parent ebac404 commit fabe4fe
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/ExNavigationComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, { PropTypes } from 'react';

import UUID from 'uuid-js';

import invariant from 'invariant';
import { createSelector } from 'reselect';
import hoistStatics from 'hoist-non-react-statics';
import PureComponent from './utils/PureComponent';
Expand Down Expand Up @@ -156,7 +157,10 @@ import {

const NavigatorPropType = PropTypes.object;

export function withNavigation<T>(WrappedComponent: ReactClass<T>) {
export function withNavigation<T>(
WrappedComponent: ReactClass<T>,
{ withRef }
) {
class WithNavigation extends PureComponent {
_wrappedInstance: ReactElement<T>;

Expand All @@ -173,9 +177,7 @@ export function withNavigation<T>(WrappedComponent: ReactClass<T>) {
render() {
return (
<WrappedComponent
ref={c => {
this._wrappedInstance = c;
}}
ref={withRef ? this.setWrappedInstance : undefined}
navigation={this.getNavigationContext()}
navigator={this.getCurrentNavigator()}
{...this.props}
Expand All @@ -191,9 +193,20 @@ export function withNavigation<T>(WrappedComponent: ReactClass<T>) {
}

getWrappedInstance() {
if (__DEV__) {
invariant(
withRef,
'To access the wrapped instance, you need to specify ' +
'{ withRef: true } in the options argument of withNavigation call.'
);
}
return this._wrappedInstance;
}

setWrappedInstance(ref) {
this._wrappedInstance = ref;
}

getNavigationContext() {
return this.props.navigation || this.context.navigation;
}
Expand Down

0 comments on commit fabe4fe

Please sign in to comment.