Successfully added
React
by Patrik
React query wrapper hook with parameter
This useArticleQuery
hook is designed to handle data fetching for articles based on the provided articleId
using the TanStack Query useQuery
function.
export const useArticleQuery = (articleId) => {
return useQuery({
queryKey: ["articles", articleId],
queryFn: () => getArticles(articleId)
});
};
The code snippet defines a custom hook called useArticleQuery
using the export
keyword, indicating it can be imported and used in other modules.
Inside the hook, the useQuery
function is called with an object as its argument. The object has two properties:
-
queryKey
: It is an array containing two elements: "articles" andarticleId
. This array is used as a key to identify the query. -
queryFn
: It is a function that gets executed to fetch the articles based on the givenarticleId
. The specific implementation ofgetArticles
is not shown in the provided code snippet.
Referenced in:
Comments