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

坚强是说给别人听的谎言 提交于 2021-02-11 15:49:08

问题


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.


回答1:


Try onCompleted like so:

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

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


来源:https://stackoverflow.com/questions/55327935/how-to-store-the-data-in-react-state-after-querying-it-from-graphql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!