Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Query callback #208

Closed
deoqc opened this issue Sep 12, 2016 · 13 comments
Closed

Query callback #208

deoqc opened this issue Sep 12, 2016 · 13 comments

Comments

@deoqc
Copy link

deoqc commented Sep 12, 2016

Is there any way to call a callback after the query has finished loading?

@jbaxleyiii
Copy link
Contributor

@deoqc you can use componentWillRecieveProps to do it!

componentWillRecieveProps(nextProps){
  if (!nextProps.data.loading && this.props.data.loading) {
    doMyCalBack();
  }
}

@deoqc
Copy link
Author

deoqc commented Sep 12, 2016

Yeah, I think I remember having in library in the past, but no problem using it in component.

Thanks!

@deoqc deoqc closed this as completed Sep 12, 2016
@richardgirges
Copy link
Contributor

richardgirges commented Mar 27, 2017

(accidentally deleted my previous comment)

I was wondering if there are any alternatives to firing a callback from componentWillReceiveProps? One issue I keep running into is not having the data on hand when firing the callback from componentWillReceiveProps.

For example:

componentWillRecieveProps(nextProps){
  if (!nextProps.data.loading && this.props.data.loading) {
    doMyCalBack();
  }
}

Then in doMyCallback():

doMyCallback() {
  const { foo } = this.props.data;

  alert(foo.message); // TypeError: Cannot read property 'message' of undefined
}

You could argue that I can just pass this.props.data.foo as an argument in the doMyCallback() method. But if there are various methods in the component, perhaps methods that can be called in multiple contexts that require usage of the data, it will send me a down the rabbit hole of passing data from method to method.

This gets kinda messy because in some cases, I can comfortably execute doMyCallback() knowing that I have the data on hand. Perhaps my approach is antonymous to the way react-apollo is meant to be used. If that's the case, can you give some pointers on how this scenario should be handled?

If the answer is to simply pass props throughout the component's methods, then I'll take it.

Thanks!

@Harokku
Copy link

Harokku commented Oct 7, 2017

Try pass nextProps to your callback and use it for your calculations.

this.props.data isn't already updated since u're inside componentWillRecieveProps

@johhansantana
Copy link

This is kind of annoying not being able to make a callback when query finishes fetching.

componentWillReceiveProps(nextProps: Props) {
  const { getProduct, setProductFiles } = nextProps
  if (!nextProps.getProduct.loading && this.props.getProduct.loading) {
    const images = []
    getProduct.Product.images.forEach(i => {
      let image = {...i}
      image.preview = i.url
      images.push(image)
    })
    setProductFiles(images)
  }
}

only works the first time the query is called, if you keep navigating around and comeback to the same route, the code inside the condition will not be called again, thus in my code, the images will not get refreshed and will show the last viewed item images.

How can I get pass this?

@lirenyeo
Copy link

@johhansantana Have you found any workaround for that issue?

@Kisepro
Copy link

Kisepro commented Mar 13, 2018

Waiting for solution too.

My problem is the same than @johhansantana with the latest packages.

@johhansantana
Copy link

Hey @lirenyeo @Kisepro I think I fixed by using the fetch policy option in Apollo

https://www.apollographql.com/docs/react/basics/queries.html#graphql-config-options-fetchPolicy

And using the option called network-only

@Kisepro
Copy link

Kisepro commented Mar 14, 2018

@johhansantana Yeah could be a nice workaround of a workaround :D

The only problem could be the performance since it's not retrieved from the cache

@birge
Copy link

birge commented Apr 23, 2018

This work around is going away with React 17, seems more than ever we will need a callback provided by Apollo.

@jlubeck
Copy link

jlubeck commented Aug 11, 2018

Should this be reopened?

@spyshower
Copy link

This reminds me of the ancient years, where each improvement/advancement came after yeeeeeears.

@shreyansh2495
Copy link

You can do something like this if you are making use of functional component
useEffect(() => {
if(data)
//do Something
}, [loading]);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests