mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-04-27 21:08:02 +08:00
Simpler implementation of withNavigation (#3476)
This will allow for refs with onRef (fixes #3461), and will avoid all these warnings from throwing during our tests
This commit is contained in:
@@ -3,16 +3,25 @@ import propTypes from 'prop-types';
|
||||
import hoistStatics from 'hoist-non-react-statics';
|
||||
|
||||
export default function withNavigation(Component) {
|
||||
const componentWithNavigation = (props, { navigation }) => (
|
||||
<Component {...props} navigation={navigation} />
|
||||
);
|
||||
class ComponentWithNavigation extends React.Component {
|
||||
static displayName = `withNavigation(${Component.displayName ||
|
||||
Component.name})`;
|
||||
|
||||
const displayName = Component.displayName || Component.name;
|
||||
componentWithNavigation.displayName = `withNavigation(${displayName})`;
|
||||
static contextTypes = {
|
||||
navigation: propTypes.object.isRequired,
|
||||
};
|
||||
|
||||
componentWithNavigation.contextTypes = {
|
||||
navigation: propTypes.object.isRequired,
|
||||
};
|
||||
render() {
|
||||
const { navigation } = this.context;
|
||||
return (
|
||||
<Component
|
||||
{...this.props}
|
||||
navigation={navigation}
|
||||
ref={this.props.onRef}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return hoistStatics(componentWithNavigation, Component);
|
||||
return hoistStatics(ComponentWithNavigation, Component);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user