Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 27x 27x 27x 27x 3x 27x 4x 3x 3x 3x 3x 3x 3x 3x 2x 2x 3x 2x 3x 3x 3x 3x 3x 3x | import * as React from 'react';
import { OperationOption } from './types';
import ApolloConsumer from './ApolloConsumer';
import { ApolloClient } from 'apollo-client';
const invariant = require('invariant');
const hoistNonReactStatics = require('hoist-non-react-statics');
function getDisplayName<P>(WrappedComponent: React.ComponentType<P>) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}
export type WithApolloClient<P> = P & { client: ApolloClient<any> };
export default function withApollo<TProps, TResult = any>(
WrappedComponent: React.ComponentType<WithApolloClient<TProps>>,
operationOptions: OperationOption<TProps, TResult> = {},
): React.ComponentClass<TProps> {
const withDisplayName = `withApollo(${getDisplayName(WrappedComponent)})`;
class WithApollo extends React.Component<TProps> {
static displayName = withDisplayName;
static WrappedComponent = WrappedComponent;
// wrapped instance
private wrappedInstance: any;
constructor(props: TProps) {
super(props);
this.setWrappedInstance = this.setWrappedInstance.bind(this);
}
getWrappedInstance() {
invariant(
operationOptions.withRef,
`To access the wrapped instance, you need to specify ` + `{ withRef: true } in the options`,
);
return this.wrappedInstance;
}
setWrappedInstance(ref: React.ComponentType<WithApolloClient<TProps>>) {
this.wrappedInstance = ref;
}
render() {
return (
<ApolloConsumer>
{client => {
const props = Object.assign({}, this.props, {
client,
ref: operationOptions.withRef ? this.setWrappedInstance : undefined,
});
return <WrappedComponent {...props} />;
}}
</ApolloConsumer>
);
}
}
// Make sure we preserve any custom statics on the original component.
return hoistNonReactStatics(WithApollo, WrappedComponent, {});
}
|