【问题标题】:How to store the data in React state after Querying it from GraphQL?从 GraphQL 查询数据后如何将数据存储为 React 状态?
【发布时间】:2019-08-15 02:41:16
【问题描述】:

以下是我的查询:

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;

我现在需要将接收到的数据存储在 React 或 Redux(无论哪个是理想的)中,以便我可以在 1/2 组件中访问该数据。如何存储数据而不是像上面那样渲染它?

根据这些数据,我正在渲染 90% 的组件。

【问题讨论】:

    标签: reactjs redux react-apollo apollo-client graphql-js


    【解决方案1】:

    像这样尝试onCompleted

    function onCompletedHandler(data) {
        dispatch({ 'RECEIVED_DATA': data })
    }
    
    <Query query={getProducts} variables={proId} onCompleted={onCompletedHandler}>
        ...
    </Query>
    

    【讨论】:

    猜你喜欢
    • 2022-08-17
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 2019-10-12
    • 2015-04-13
    • 1970-01-01
    • 2022-08-05
    相关资源
    最近更新 更多