Query key uses conditional value #5067
-
const { currentUser } = useLamaAuth()
// if (!currentUser) return null
const { data: user } = useQuery<IUser>({
queryKey: ["user", currentUser?.id],
queryFn: () => api.get("/users").then((response) => response.data),
staleTime: 1000 * 60, // 1 minuto
enabled: !!currentUser // ???
}) i'm trying to fetch user data from api, and setting a unique query key for each user (is the profile page of each user logged in), but the user can be null, when there is no one logged in (i'm using context api to provide user data to all components, if there is a better way to do it, feel free to talk about it, in the example above, i'm using if currentUser is null, i don't want to fetch any data, not even create another cache, but the results i'm getting is either another query with ["user", null], which isn't desired, or an error saying |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
your code looks fine. Don't worry about the |
Beta Was this translation helpful? Give feedback.
your code looks fine. Don't worry about the
["user", null]
query.