Mat
Mat

Reputation: 51

ReactJS + Apollo Client - Get result without rendering

I use ReactJS Typescript with Apollo Client.

I'm trying to know how to make a request to my server and store the result in a variable without generating a component?

I can do that :

<Query query={GET_AC}>
{({ loading, data }) => {
if (loading) return (<div>Loading</div>);
return (data.truc.map((ac) => <div>ac</div>));
</Query>

But I have to make a return with a component.

I would like to store the result in a variable of my component (and then make a live search system) without processing the result. How can I do that?

Like : var result = query(maQuery); // In my component Class

Thanks !

Upvotes: 0

Views: 246

Answers (2)

ipinlnd
ipinlnd

Reputation: 175

you can use Apollo's hooks

var result = useQuery(query)

https://www.apollographql.com/docs/react/data/queries/#executing-a-query

There's also another way which isn't very common, export your apollo client, and simply call the query function of it:

client.query({query: yourQuery})

Upvotes: 0

Oleg Kupriianov
Oleg Kupriianov

Reputation: 178

You can get data as prop by using Apollo HOC graphql or by using hook useQuery inside functional component.

Upvotes: 1

Related Questions