Search code examples
reactjsgraphqlapollo

How to use Apollo's multiple useQuery() hooks in React


I need to use 2 queries in my file and I am writing them like so:

const {loading, data } = useQuery(getCharactersQuery);
const {loading, data} = useQuery(getSingleCharacterQuery);

The problem is, they both have the same "loading" and "data" variables and I don't see anywhere in the docs how can we have different ones. How can I differentiate them?


Solution

  • It's Object destructuring of JS Destructuring assignment. You can choose not to use it here to give different variable names.

    const resCharacters = useQuery(getCharactersQuery);
    const resSingleCharacter = useQuery(getSingleCharacterQuery);
    
    if (resCharacters.loading || resSingleCharacter.loading) return 'Loading...';
    ...