Files
react-navigation/src/views/withNavigation.js
Eric Vicenti ecd9fd71e9 withNavigation improvement (#3834)
The navigation prop should also pass through, and be prioritized over context because it is more explicit

This also fixes an incorrect warning/invariant
2018-03-25 10:49:09 -07:00

37 lines
1.2 KiB
JavaScript

import React from 'react';
import propTypes from 'prop-types';
import hoistStatics from 'hoist-non-react-statics';
import invariant from '../utils/invariant';
import { NavigationConsumer } from './NavigationContext';
export default function withNavigation(Component) {
class ComponentWithNavigation extends React.Component {
static displayName = `withNavigation(${Component.displayName ||
Component.name})`;
render() {
const navigationProp = this.props.navigation;
return (
<NavigationConsumer>
{navigationContext => {
const navigation = navigationProp || navigationContext;
invariant(
!!navigation,
'withNavigation 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.'
);
return (
<Component
{...this.props}
navigation={navigation}
ref={this.props.onRef}
/>
);
}}
</NavigationConsumer>
);
}
}
return hoistStatics(ComponentWithNavigation, Component);
}