Just a simple query here... my goal is to extract data.user.roles
, but there's a possibility that data
may be empty. In such cases, I want an empty array as the output. Additionally, I need to specify the type of user
- which in this instance is any
.
This is the solution I have devised:
const { data } = useSession()
const user: any = data?.user || {} // defining user
const roles = user?.roles || []
It does the job, but it doesn't seem very efficient.
Moreover, I'm uncertain if this approach will hold up if useSession
doesn't fetch the complete dataset:
type Session = {
data: {
user?: {
roles?: string[]
}
}
}
const {
data: {
user: { roles = [] }
}
}: Session = useSession()