-
Notifications
You must be signed in to change notification settings - Fork 787
[Feature] add prop onResolved={data => xxx}
to Query
#1947
Comments
Especially in regards to the |
As a workaround I've set <Query
query={query}
notifyOnNetworkStatusChange>
{(args) => {
const { loading, data, networkStatus } = args;
if (!loading && this.lastNetworkStatus !== networkStatus) {
this.onChange(args); // Your callback here
this.lastNetworkStatus = networkStatus;
}
</Query> |
My use case is: <Query
query={query}
variables={variables}
onComplete={({ query, variables, data, error }) => do_some_side_effects()}
>
{({ loading, error, data }) => show_the_ui({ loading, error, data })}
</Query> Because do side effect in render function is not good. So, maybe the best way is to give a prop |
I realize that my previous post may have not been clear, but I do agree that it would be far more convenient for Apollo to provide a callback for a network status change or an onCompleted callback like the one on the Mutation component. That said you should still be able to accomplish your desired effect using a custom callback in the render function. Do be aware that the render function does get recalled every time the network status changes or new props are passed in. The only main drawback to making your own callback is that changing state inside the render function is dangerous as it can lead to infinite rerendering loops if you aren't careful. |
In my use case I have an infinite scroll list with each item having a checkbox. I also have a list control to select/unselect all of the checkboxes that has it's value controlled by a state. I needed a way to change the value of the select all after getting new items. Without some kind of callback this functionality is awkward at best requiring a half-controlled-half-uncontrolled checkbox. onChange(args) {
clearInterval(this.changeTimer);
this.props.onChange(args); // Your callback here
}
render() {
return (
<Query
query={query}
notifyOnNetworkStatusChange>
{(args) => {
const { loading, data, networkStatus } = args;
if (!loading && this.lastNetworkStatus !== networkStatus) {
this.changeTimer = setInterval(this.onChange, 0, args);
this.lastNetworkStatus = networkStatus;
}
}}
</Query>
)
} |
PR made several months ago for this feature: #1922 |
┓( ´∀` )┏ |
PR #1922 has been merged (and will be included in the next |
@hwillson I assume you mean *July 3rd? Or does Apollo time travel? |
I am asking my self the same too :) |
Yes definitely - thanks for catching that @jarommadsen @windgaucho! I guess I'm not quite ready for July yet ... ⏲ |
Use some concept in redux to describe this question:
So, how can we do
3
?graphql(queyr_document, options)
and<Query />
are bothdeclarative programming
, but some times we needimperative programming
...If I use
client.query()
incomponentDidMount
, then I lost the goods fromdeclarative programming
(subscribing to later changes)...Maybe add API
onResolved={data => xxx}
or whatever name is OK.Of course,
graphql(queyr_document, options)
need it too.The text was updated successfully, but these errors were encountered: