TanStackT
TanStackโ€ข3y agoโ€ข
2 replies
verbal-lime

Is enriching the query-return props a good practice?

Hey ๐Ÿ‘‹๐Ÿฝ ,
I'm interested in your opinion. Please check out following code:

// use-current-user.js
export function useCurrentUser() {
  const query = useQuery({
    queryKey: ["users", "detail", "me"],
    staleTime: 1000 * 1800,
    queryFn: () => rq(currentUserQuery).then((data) => data.currentUser),
  });

  return {
    ...query,
    currentUser: query.data,
    hasRightFn: (right) =>
      query.isSuccess ? query.data.rights.includes(right) : false,
  };
}

function MyComponent() {
  const { hasRightFn } = useCurrentUser();

  return (
    <div>
      {hasRightFn("refund_allowed") ? (
        <Button>Refund</Button>
      ) : null}
    </div>
  )
}

Here the return properties are enriched by 2 further properties (hasRightFn and currentUser). Are there any disadvantages here? I think no.

thanks
Was this page helpful?