-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(docs): Add loading prop usage on Connect component and mutation code snippet #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -440,8 +440,8 @@ class App extends React.Component { | |
|
|
||
| return ( | ||
| <Connect query={graphqlOperation(ListEvents)}> | ||
| {({ data: { listEvents } }) => ( | ||
| <ListView events={listEvents.items} /> | ||
| {({ data: { listEvents }, loading }) => ( | ||
| !loading && <ListView events={listEvents.items} /> | ||
| )} | ||
| </Connect> | ||
| ) | ||
|
|
@@ -461,8 +461,8 @@ Also, you can use `subscription` and `onSubscriptionMsg` attributes to enable su | |
| onSubscriptionMsg={(prev, { subscribeToEventComments }) => { | ||
| console.log ( subscribeToEventComments); | ||
| return prev; }}> | ||
| {({ data: { listEvents } }) => ( | ||
| <AllEvents events={listEvents ? listEvents.items : []} /> | ||
| {({ data: { listEvents }, loading }) => ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the above, let's be more prescriptive on the error handling and also call out the function argument from the GraphQL response. Here is a suggestion for the code and text: Note - In the above example |
||
| !loading && <AllEvents events={listEvents ? listEvents.items : []} /> | ||
| )} | ||
| </Connect> | ||
|
|
||
|
|
@@ -471,16 +471,60 @@ Also, you can use `subscription` and `onSubscriptionMsg` attributes to enable su | |
| For mutations, a `mutation` function needs to be provided with `Connect` component. `mutation` returns a promise that resolves with the result of the GraphQL mutation. | ||
|
|
||
| ```js | ||
| class CreateEvent extends React.Component { | ||
| // ... | ||
| // This component calls its onCreate prop to trigger the mutation | ||
| // const variables = { id: '1' } | ||
| // this.props.onCreate(variables) | ||
| // ... | ||
| class CreateEventView extends React.Component { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe since this is an example we can simplify it a bit so that new customers to React don't get lost on the extra state management here for handling input. Also, we should show the |
||
|
|
||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.state = this.getInitialState(); | ||
| } | ||
|
|
||
| getInitialState() { | ||
| return { | ||
| name: '', | ||
| where: '', | ||
| when: new Date().toISOString(), | ||
| description: '', | ||
| }; | ||
| } | ||
|
|
||
| handleChange = (event) => { | ||
| this.setState({ [event.target.name]: event.target.value }); | ||
| } | ||
|
|
||
| handleSubmit = async () => { | ||
| const { name, where, when, description } = this.state; | ||
| const { onCreate } = this.props; | ||
|
|
||
| try { | ||
| await onCreate({ name, where, when, description }); | ||
| } catch (error) { | ||
| console.error(error); | ||
| } | ||
|
|
||
| this.setState(this.getInitialState()); | ||
| } | ||
|
|
||
| render() { | ||
| const { name, where, when, description } = this.state; | ||
|
|
||
| return ( | ||
| <div onSubmit={this.handleSubmit}> | ||
| <div> | ||
| <label>Create event</label> | ||
| <div><label>Name</label> <input type="text" name="name" value={name} onChange={this.handleChange} /></div> | ||
| <div><label>Where</label> <input type="text" name="where" value={where} onChange={this.handleChange} /></div> | ||
| <div><label>When</label> <input type="text" name="when" value={when} onChange={this.handleChange} /></div> | ||
| <div><label>Description</label><input type="text" name="description" value={description} onChange={this.handleChange} /></div> | ||
| <button onClick={this.handleSubmit}>Create</button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
| <Connect mutation={graphqlOperation(Operations.CreateEvent)}> | ||
| {({ mutation }) => ( | ||
| <CreateEvent onCreate={mutation} /> | ||
| <CreateEventView onCreate={mutation} /> | ||
| )} | ||
| </Connect> | ||
| ``` | ||
|
|
||
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.
Can we do something a little more comprehensive that also includes error checking like so:
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.
For sure, I will try this and update the docs. Thanks!
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.
Here is the functioning snippet in JSX: