Merge pull request #30 from bySabi/master

prevent onRef callback be called twice on withNavigationFocus Components
This commit is contained in:
Brent Vatne
2019-01-28 13:20:35 -08:00
parent baf5a90dd0
commit f35c3c2513
2 changed files with 9 additions and 9 deletions

View File

@@ -3,7 +3,10 @@ import hoistStatics from 'hoist-non-react-statics';
import invariant from '../utils/invariant';
import NavigationContext from './NavigationContext';
export default function withNavigation(Component) {
export default function withNavigation(
Component,
config = { forwardRef: true }
) {
class ComponentWithNavigation extends React.Component {
static displayName = `withNavigation(${Component.displayName ||
Component.name})`;
@@ -22,7 +25,7 @@ export default function withNavigation(Component) {
<Component
{...this.props}
navigation={navigation}
ref={this.props.onRef}
ref={config.forwardRef ? this.props.onRef : undefined}
/>
);
}}

View File

@@ -1,6 +1,5 @@
import React from 'react';
import hoistStatics from 'hoist-non-react-statics';
import invariant from '../utils/invariant';
import withNavigation from './withNavigation';
export default function withNavigationFocus(Component) {
@@ -18,11 +17,6 @@ export default function withNavigationFocus(Component) {
componentDidMount() {
const { navigation } = this.props;
invariant(
!!navigation,
'withNavigationFocus can only be used on a view hierarchy of a navigator. The wrapped component is unable to get access to navigation from props or context.'
);
this.subscriptions = [
navigation.addListener('didFocus', () =>
this.setState({ isFocused: true })
@@ -48,5 +42,8 @@ export default function withNavigationFocus(Component) {
}
}
return hoistStatics(withNavigation(ComponentWithNavigationFocus), Component);
return hoistStatics(
withNavigation(ComponentWithNavigationFocus, { forwardRef: false }),
Component
);
}