React Basic Usage
Using GraphQL data within React components is straightforward.
- Updates components when data changes
- (When enabled) Suspends components while data is being fetched
#
PrerequisitesMake sure you've completed Getting Started first
"../gqless"
refers to the generated client file/directory
#
InstallationFirst install @gqless/react
into your project's dependencies:
- pnpm
- npm
- yarn
#
UsageIn your previously generated client, you should have something like this:
You can remove some hooks / functions that you are not planning to use
For more information about the configuration of the client check here.
caution
We recommend you reading the caveats of React Usage
We can take a quick look at all those functions:
#
Query relatedFor more information check here.
- useQuery | Main hook that uses all the main design of
gqless
.
- graphql | HOC (Higher-Order Component) designed specially for Suspense usage.
- useTransactionQuery | Alternative to
graphql
anduseQuery
that works via pre-defined functions, which allows for extra features that are not available in the previously mentioned functions, like for example, scoped queries, polling, conditional skipping.
- useLazyQuery | Queries meant to be called in response of events, like button clicks.
- useRefetch | Refetch giving specific parts of the schema or via functions.
- prepareQuery | Prepare queries on module-level, allowing for the famous Render as you fetch pattern, re-using the callbacks in useTransactionQuery & useLazyQuery, composition, prefetching, and more.
#
Mutation relatedFor more information check here.
- useMutation | Hook designed to be used for GraphQL Mutations.
#
Subscription relatedFor more information check here.
- useSubscription | Hook designed to be used for GraphQL Subscriptions.
#
Meta relatedFor more information check here.
- useMetaState | Hook designed to inspect the React Client state, like error handling and fetching state.
#
SSR relatedFor more information check here.
Examples made for Next.js SSR Data Fetching
- prepareReactRender - Function designed to be used before the React Render is being made, for example, in places like getStaticProps or getServerSideProps of Next.js.
- useHydrateCache - Function designed to be placed at the very top of the hydrated component.