What is @apollo/react-hooks?
@apollo/react-hooks is a library that provides React hooks for interacting with Apollo Client, a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. This package allows you to easily integrate GraphQL queries, mutations, and subscriptions into your React components using hooks.
What are @apollo/react-hooks's main functionalities?
useQuery
The `useQuery` hook is used to fetch data from a GraphQL endpoint. It takes a GraphQL query as an argument and returns an object containing the loading state, any errors, and the fetched data.
import { useQuery } from '@apollo/react-hooks';
import gql from 'graphql-tag';
const GET_DOGS = gql`
query GetDogs {
dogs {
id
breed
}
}
`;
function Dogs() {
const { loading, error, data } = useQuery(GET_DOGS);
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>;
return (
<ul>
{data.dogs.map(dog => (
<li key={dog.id}>{dog.breed}</li>
))}
</ul>
);
}
useMutation
The `useMutation` hook is used to perform GraphQL mutations. It takes a GraphQL mutation as an argument and returns a tuple containing the mutate function and an object with the mutation result.
import { useMutation } from '@apollo/react-hooks';
import gql from 'graphql-tag';
const ADD_DOG = gql`
mutation AddDog($breed: String!) {
addDog(breed: $breed) {
id
breed
}
}
`;
function AddDog() {
let input;
const [addDog, { data }] = useMutation(ADD_DOG);
return (
<div>
<form
onSubmit={e => {
e.preventDefault();
addDog({ variables: { breed: input.value } });
input.value = '';
}}
>
<input
ref={node => {
input = node;
}}
/>
<button type="submit">Add Dog</button>
</form>
</div>
);
}
useSubscription
The `useSubscription` hook is used to subscribe to GraphQL subscriptions. It takes a GraphQL subscription as an argument and returns an object containing the subscription data and loading state.
import { useSubscription } from '@apollo/react-hooks';
import gql from 'graphql-tag';
const DOG_ADDED = gql`
subscription OnDogAdded {
dogAdded {
id
breed
}
}
`;
function DogSubscription() {
const { data, loading } = useSubscription(DOG_ADDED);
if (loading) return <p>Loading...</p>;
return <p>New dog added: {data.dogAdded.breed}</p>;
}
Other packages similar to @apollo/react-hooks
react-apollo
The `react-apollo` package is the predecessor to `@apollo/react-hooks` and provides higher-order components (HOCs) and render props for integrating Apollo Client with React. While it offers similar functionality, `@apollo/react-hooks` leverages React hooks for a more modern and concise API.
urql
`urql` is a lightweight GraphQL client for React that provides hooks for querying, mutating, and subscribing to GraphQL data. It is similar to `@apollo/react-hooks` but is designed to be more modular and extensible, allowing developers to customize its behavior more easily.
relay-hooks
`relay-hooks` is a library that provides React hooks for Relay, a GraphQL client developed by Facebook. It offers similar functionality to `@apollo/react-hooks` but is designed to work with the Relay framework, which includes advanced features like data-driven dependencies and optimistic updates.