Next.js Discord

Discord Forum

component & useEffect for ContextProvider not called.

Unanswered
American black bear posted this in #help-forum
Open in Discord
Avatar
American black bearOP
Hi,

In order to load a user session on client side, we tried to introduce a context provider. Unfortunately the component is never called, but the pages show. Is that some kind of hydration problem?

export type UserContext = {
    user?: User
    error?: Error
    isLoading: boolean
    isInitialized: boolean
}

export type UserContextMethods = {
    reload: () => void
}

export const ClientUserContext = createContext<UserContext & UserContextMethods>({
    isLoading: false,
    isInitialized: false,
    reload: () => void 0,
})

export const UserProvider: FC<PropsWithChildren> = ({ children }) => {
    console.log('UserProvider')
    const [userState, setUserState] = useState<UserContext>({
        isLoading: false,
        isInitialized: false,
    })
    const userContext = useMemo(
        () => ({
            ...userState,
            reload: () => {
                setUserState((s) => ({ ...s, ...loadAndSet(), isInitialized: true }))
                loadAndSet()
            },
        }),
        [userState, setUserState],
    )
    return <ClientUserContext.Provider value={userContext}>{children}</ClientUserContext.Provider>
}

0 Replies