-
Notifications
You must be signed in to change notification settings - Fork 786
Implemented the ApolloConsumer component. #1399
Implemented the ApolloConsumer component. #1399
Conversation
Style-wise you could use |
Generated by 🚫 dangerJS |
Hey @rosskevin, this is indeed a style issue. I know that react router uses the "render" prop pattern for example. I will leave it the way it is for now and wait for feedback from others:) |
# Conflicts: # Changelog.md
I updated the component to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just one thing on the return type.
const invariant = require('invariant'); | ||
|
||
export interface ApolloConsumerProps { | ||
children: (client: ApolloClient<any>) => React.ReactElement<any>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the return should React.ReactNode
- it is looser than ReactElement
and we have no real concerns about the return type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, as soon as I update the type to React.ReactNode
I get the typescript error:
Type '(props: ApolloConsumerProps & { children?: ReactNode; }, context: any) => ReactNode' is not assignable to type 'StatelessComponent<ApolloConsumerProps>'.
Type 'ReactNode' is not assignable to type 'ReactElement<any>'.
Type 'string' is not assignable to type 'ReactElement<any>'.
It seems that StatelessComponent not accept a ReactNode
as the return type. Any ideas on how to fix this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's mistake then, it should match render's return type. I was thinking it would be node because 16 allows arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should say my mistake - I'm mobile
The roadmap for v2.1 describes moving to a component based API (
Query
,Mutation
andSubscription
) in addition to the HOCs.I implemented the component associated with the existing HOC
withApollo
calledApolloConsumer
. This will allow users access to the apollo-client instance using a render prop as follows:I got my inspiration from react-router's API. They have a Route component and a withRouter HOC. The implementation of the HOC is simply a wrapper around the
Route
component. The same could be done forwithApollo
, it can simply wrap theApolloConsumer
.Curious to hear how people feel about such a component and the API proposed above.