Reactjs 如何在从GraphQL查询数据后将其存储为React状态?
以下是我的疑问:Reactjs 如何在从GraphQL查询数据后将其存储为React状态?,reactjs,redux,react-apollo,apollo-client,graphql-js,Reactjs,Redux,React Apollo,Apollo Client,Graphql Js,以下是我的疑问: import React from 'react'; import getProducts from '../queries/getProduct'; import { Query } from "react-apollo"; export const Product = () => { const proId = { "productId": "95eb601f2d13" } return ( <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;
我现在需要将接收到的数据存储在React或Redux中,以理想的方式存储,这样我就可以访问1/2组件中的数据。如何存储数据而不是像上面那样渲染数据
根据这些数据,我渲染了90%的组件 尝试这样完成:
我还建议您查看一下阿波罗客户的当地州管理。
function onCompletedHandler(data) {
dispatch({ 'RECEIVED_DATA': data })
}
<Query query={getProducts} variables={proId} onCompleted={onCompletedHandler}>
...
</Query>