rsnext/examples/with-passport-and-next-connect/lib/hooks.jsx
2023-01-12 09:36:29 -08:00

11 lines
346 B
JavaScript

import useSWR from 'swr'
export const fetcher = (url) => fetch(url).then((r) => r.json())
export function useUser() {
const { data, mutate, isLoading } = useSWR('/api/user', fetcher)
// if data is not defined, the query has not completed
const loading = isLoading || !data
const user = data?.user
return [user, { mutate, loading }]
}