问题
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