mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-02-12 22:30:36 +08:00
Merge pull request #30 from bySabi/master
prevent onRef callback be called twice on withNavigationFocus Components
This commit is contained in:
@@ -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}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user