Next.js Discord

Discord Forum

Invalid Hook Call Warning in Next.js with pnpm, Kinde Auth, ZenStack, and React Query on Windows 11

Unanswered
Selkirk Rex posted this in #help-forum
Open in Discord
Selkirk RexOP
I'm encountering an "Invalid Hook Call" warning in my Next.js app. The error suggests that hooks are being called outside the body of a function component. Here are the details:

- Tech Stack: pnpm, Next.js, Kinde Auth, ZenStack, React Query
- Environment: Windows 11
- Error:
   Warning: Invalid hook call. Hooks can only be called inside the body of a function component.
   TypeError: Cannot read properties of null (reading 'useContext')
   at t.useContext
   


Steps I've taken:
1. Ensured matching versions of React and React DOM
2. Checked the rules of hooks
3. Verified there's only one copy of React

Any guidance on debugging this would be helpful. Thank you!

50 Replies

@gin show me your component where u call the hook
Selkirk RexOP
Hmm, the problem is i dont know exactly which component throws the error
but let me see
again
@gin it should give u the stack trace
Selkirk RexOP
u mean that
?
this is not even understandable
do u have a context that u use?
Selkirk RexOP
yep i have a few contexts
and providers
if u mean that
then check for any implementation of your context in your components
and make sure u call it only in react components
Selkirk RexOP
i am using only react components actually
i thought this is more Nextjs bug
than mine
Selkirk RexOP
yeah usePathname is a client only react hook
@gin wdym by that
Selkirk RexOP
i am not using classes and etc
@gin yeah usePathname is a client only react hook
Selkirk RexOP
yep i checked if i use it
and?
Selkirk RexOP
and i am using it only on client side
show me the code
where u use it
Selkirk RexOP
if it is the cause of the problem
it should be provider
component
"use client";

import { usePathname, useRouter } from "next/navigation";
import React, { createContext, useContext, useTransition } from "react";
// import { usePathname, useRouter } from "../i18n";
type LoadingContextType = {
  push: (url: string, locale?: "en" | "ka", doRefresh?: boolean) => void;
  make: (fn: () => Promise<void>) => void;
  isLoading: boolean;
};

const LoadingContext = createContext<LoadingContextType | undefined>(undefined);

export const useLoading = () => {
  const context = useContext(LoadingContext);
  if (!context) {
    throw new Error("useLoading must be used within an LoadingProvider");
  }
  return context;
};

type LoadingProviderProps = {
  children: React.ReactNode;
};

export function LoadingProvider({ children }: LoadingProviderProps) {
  const router = useRouter();
  const [isLoading, startTransition] = useTransition();
  const pathname = usePathname();
  function push(url: string, locale?: "en" | "ka", doRefresh?: boolean) {
    if (url === pathname) {
      return;
    }
    startTransition(() => {
      router.push(url);
      if (doRefresh) {
        router.refresh();
      }
    });
  }

  function make(fn: () => Promise<void>) {
    startTransition(async () => {
      await fn();
    });
  }

  return (
    <LoadingContext.Provider value={{ push, make, isLoading }}>
      {isLoading && (
        <div className="w-full fixed top-0 left-0 z-[999]">
          <div className="h-1.5 w-full bg-primary/10 overflow-hidden">
            <div className="progress w-full h-full bg-primary left-right"></div>
          </div>
        </div>
      )}
      {children}
    </LoadingContext.Provider>
  );
}
i have to remove this component in future but
i dont understand why that can be cause
becuase i am not using usePathname on any other component
and ur LoadingProvider is in your layout right
Selkirk RexOP
yep
now i will test it
i will remove it
and where are u using useLoading?
@gin and where are u using useLoading?
Selkirk RexOP
actually i am not using it on the page
xD
and now the error is not here
ahh i dont get it
sorry i am wasting your time right now
i will research more about that
it can be even windows and pnpm problem
who knows
i mean i had the problem in the morning today
right now there is no error
thanks for your help