Useful Hooks For Data Fetching With Faust.js
When you use getClient from Faust.js to generate a GQty client, you will get some additional hooks that you can use to make your client more useful and make fetching data from Headless WordPress easier. The following hooks are available on the client object generated by getClient.
usePost#
The usePost hook provides the standard interface for getting a post from your Headless WordPress API. It also allows you to pass-in no arguments. When you do not pass arguments into usePost it will attempt to look at the URL params in order to determine how to get a post from your Headless WordPress API. In Next.js this means you will need to name your post page file in a particular way in order to get the proper URL params. Below are the possible names that will work automatically with Faust.js:
[postId].tsx[postSlug].tsx[...postUri].tsx
Using the above names, Faust.js is able to apply the following logic to determine how to get a post from your Headless WordPress API:
- If
postIdis found in the URL params, Faust.js makes a request to retrieve apostfrom WordPress byID - If
postSlugis found in the URL params, Faust.js makes a request to retrieve apostfrom WordPress bySLUG - If
postUriis found in the URL params, Faust.js makes a request to retrieve apostfrom WordPress byURI
The following is an example of how to use the usePost hook with a postSlug:
The above code will also work with postId and postUri depending upon what URL scheme you want to use. You may also want to fetch a specific post. Doing that might look similar to the following:
usePosts#
The usePosts hook provides the standard interface for getting a list of posts from your Headless WordPress API. It also allows you to pass-in no arguments. When you do not pass arguments into usePosts it will attempt to look at the URL params in order to determine how to get a list of posts from your Headless WordPress API. In Next.js this means you will need to name your post page file in a particular way in order to get the proper URL params. Below are the possible names that will work automatically with Faust.js:
- If
categoryIdis found in the URL params, a request is made to retrieve a list of posts for acategorybyID - If
categorySlugis found in the URL params, a request is made to retrieve a list of posts for acategorybySLUG - If no URL params are found, a request is made to retrieve a list of posts without any filters
The following is an example of how to use the usePosts hook with no URL params:
The code above will get the first 6 posts from the Headless WordPress API. If you want to create a page that would pull posts associated with a specific category you can use the following code:
The code above will get the first 6 posts from the Headless WordPress API that are for the categorySlug defined in the URL params.
For an example of usePosts with pagination, take a look at our Next.js getting started example.
usePage#
The usePage hook provides the standard interface for getting a page from your Headless WordPress API. It also allows you to pass-in no arguments. When you do not pass arguments into usePage it will attempt to look at the URL params in order to determine how to get a page from your Headless WordPress API. In Next.js this means you will need to name your page file in a particular way in order to get the proper URL params. Below are the possible names that will work automatically with Faust.js:
[pageId].tsx[...pageUri].tsx
Using the above names, Faust.js is able to apply the following logic to determine how to get a page from your Headless WordPress API:
- If
pageIdis found in the URL params, Faust.js makes a request to retrieve apagefrom WordPress byID - If
pageUriis found in the URL params, Faust.js makes a request to retrieve apagefrom WordPress byURI
The following is an example of how to use the usePage hook with a pageUri:
The above code will also work with pageId and pageUri depending upon what URL scheme you want to use. You may also want to fetch a specific page. Doing that might look similar to the following:
usePreview#
The usePreview hook provides an abstraction around getting a preview page or post from your Headless WordPress API. WordPress generally uses query parameters for post and page preview URLs. It also uses the ID of the post or page, so you need to call the Headless WordPress API in a specific way to get the data you need. You also can't use the Next.js URL params scheme with dynamic file names to read query params. Instead you have to pull the query params off the Next.js router and pass them into usePreview. usePreview expects you to pass in either pageId or postId depending upon what type of content you are fetching. If you pass in pageId then usePreview will look for a page by ID. If you pass in postId then usePreview will look for a post by ID.
The following example shows how to use the usePreview hook to render either a post or page from WordPress:
Custom Queries and Mutations#
GQty publishes the following hooks that can be used for custom queries, mutations, or subscriptions:
useQuery- Make any query request to the Headless WordPress API
useLazyQueryuseTransactionQueryuseMutation- Make any mutation request to the Headless WordPress API
useSubscription
For example, you may want to get a list of your content types:
useQuery, along with all the other hooks, are typed. So you'll be able to see exactly what kind of data you have access to via your IDE's intellisense.