question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to update props after this.props.client.query via 'withApollo' HOC ?

See original GitHub issue

My use case is that I do not want to do the initial query for search component, instead of that, I will trigger query after button click, seems like the only choice is withApollo HOC.

Here is my code fragment.

    this.props.client.query({
      query: doc,
      variables: variant,
      reducer: (previousResult, action, variables) => {
        console.log('reducer!!!!!', previousResult, action, variables);
      }
    }).then((data) => {
      console.log('then', data, this.props)
    }).catch((err) => {
      console.log('catch', err)
    });

Fetching data is fine, I can get data in promise then, but how I can update it into props ? As the document http://dev.apollodata.com/core/apollo-client-api.html#ApolloClient\.query , reducer function should do that, but unfortenately it never be triggered in my sample code.

Did I do anything wrong? Or there is the other solution to solve that?

PS: version react-apollo@1.0.0-rc.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
helfercommented, Mar 28, 2017

@smilexu Reducer is not the right choice for this. You should be able to use client.writeQuery or client.writeFragment to update the cache.

Maybe a cleaner way to do this in React would be to have a Search component with a text field, then a searchResult component that’s nested inside it. The searchResult component gets the search text as a prop. That way you could directly attach the query to the searchResult component.

1reaction
Elliryannacommented, Jul 25, 2017

have you already tried to reset the store, after calling the “first” login/error this.props.client.resetStore(); maybe it helps, its for me a kind of solution, but not in every case i have

Read more comments on GitHub >

github_iconTop Results From Across the Web

HOC - Apollo GraphQL Docs
Use this function to create higher-order components that can execute queries and update reactively based on the data in your Apollo store. The...
Read more >
How to update a separate components props after performing ...
I want to re-render Header when username changes. I'm not sure how to do this without polling the query. javascript · reactjs ·...
Read more >
Dissecting React Apollo Render Props | by Marc G - Medium
Apollo comes with an HOC named withApollo which exposes the client object for our FakeQuery component to utilize via it's props and query...
Read more >
React with Apollo and GraphQL Tutorial - Robin Wieruch
GraphQL Caching of Queries with Apollo Client in React ... It uses React's render props pattern, using a child as a function implementation ......
Read more >
Tutorial: Render Props in React Apollo 2.1 - Prisma
Generally, the new render prop components Query, Mutation and ... started quickly with Apollo Client without much configuration overhead.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found