#Destructuring not working

8 messages · Page 1 of 1 (latest)

calm grove
#
interface DashboardContextProps {
    user: { name: string }
}

export const DashboardContext = createContext<DashboardContextProps | null>(null);

export const UseDashboardContext = () => {
    return useContext(DashboardContext)
}

And now when I want to destructure "user" from UseDashboardContext it gives me error:
"Property user does not exist on type DashboardContextProps | null "

Why is that?
Thank you for your effort

#

const { user } = UseDashboardContext() this is what i try to do

tiny nebula
calm grove
#

so how do i fix it? Or i have to do it this way always:

const data = UseDashboardContext()
console.log(data.user)
tiny nebula
#

not possible because that's not type safe

#

you can const context = useContext
into a variable

#

and throw an error if its null

calm grove
#

thank you teacher