Ishan Patel
Ishan Patel

Reputation: 6091

How to store the data in React state after Querying it from GraphQL?

Following is my Query:

import React from 'react';
import getProducts from '../queries/getProduct';
import { Query } from "react-apollo";


export const Product = () => {
    const proId = {
        "productId": "95eb601f2d13"
    }

   return (
       <Query query={getProducts} variables={proId}>
        {({ loading, error, data }) => {
            if (loading) return "Loading...";
            if (error) return `Error! ${error.message}`;

            return (
                <div>
                    {data.map(entry => entry)}
                </div>
            );
        }}
       </Query>
   );
};

export default Product;

I need to now store the received data in React or Redux (whichever is ideal) so I can access that data in 1/2 Components. How can I store data rather than rendering it like it's doing above?

Depending on this data, I am rendering 90% components.

Upvotes: 1

Views: 652

Answers (1)

Hrayr Movsisyan
Hrayr Movsisyan

Reputation: 34

Try onCompleted like so:

function onCompletedHandler(data) {
    dispatch({ 'RECEIVED_DATA': data })
}

<Query query={getProducts} variables={proId} onCompleted={onCompletedHandler}>
    ...
</Query>

Upvotes: 1

Related Questions