Next.js Discord

Discord Forum

onClick is not working

Unanswered
Tiphiid wasp posted this in #help-forum
Open in Discord
Tiphiid waspOP
Anyone has idea why onClick function is not working in nextjs of my code
Am I missing something?

65 Replies

@!=tgt What's your code in question?
Tiphiid waspOP
onClick is not working anywhere in the code
Tiphiid waspOP
<div className="flex">
            <Image
              src="/assets/images/logo.svg"
              width={37}
              height={37}
              className="rounded-full hover:bg-sky-900"
              alt="logo"
              onClick={() => {
                handleToggle;
              }}
            />
            {toggleDropdown && (
              <div className="dropdown">
                <Link
                  href="/profile"
                  className="dropdown_link"
                  onClick={() => setToggleDropdown(false)}
                >
                  My Profile
                </Link>
                <Link
                  href="/create-prompt"
                  className="dropdown_link"
                  onClick={() => setToggleDropdown(false)}
                >
                  Create Prompt
                </Link>
                <button
                  type="button"
                  onClick={() => {
                    setToggleDropdown(false);
                    signOut();
                  }}
                  className="mt-5 w-full black_btn"
                >
                  Sign Out
                </button>
              </div>
            )}
          </div>
@!=tgt Send an example
Tiphiid waspOP
In image tag u can consider as a example
Tiphiid waspOP
I was actually logging there
even that is not working
 <Image
   src="/assets/images/logo.svg"
   width={37}
   height={37}
   className="rounded-full hover:bg-sky-900"
   alt="logo"
   onClick={handleToggle}
 />

this is the correct way
since it's a named function
@Tiphiid wasp yup even tried that earlier
any console logs?
@!=tgt any console logs?
Tiphiid waspOP
no nothing
hm
what does HandleToggle do?
send the function for that
Tiphiid waspOP
nothing dude, it has a simple log which is not working
const handleToggle = () => {
console.log('sss')
}
@!=tgt hm
Tiphiid waspOP
i m also getting a error when i use body tag inside layout.tsx
@Tiphiid wasp const handleToggle = () => { console.log('sss') }
try doing this
 <Image
   src="/assets/images/logo.svg"
   width={37}
   height={37}
   className="rounded-full hover:bg-sky-900"
   alt="logo"
   onClick={()=>handleToggle()}
 />
@!=tgt What is it?
Tiphiid waspOP
Application error: a client-side exception has occurred (see the browser console for more information).
@!=tgt and what's in the browser console?
Tiphiid waspOP
also try deleting the /.next folder
@Tiphiid wasp Click to see attachment
try expanding the error
there should be an error telling you where the problem is
Tiphiid waspOP
@Tiphiid wasp Click to see attachment
Tiphiid waspOP
while using body tag in layout.tsx
@Tiphiid wasp while using body tag in layout.tsx
what's your layout.tsx
Tiphiid waspOP
// "use client";
import Nav from "@components/Nav";
import "@styles/globals.css";
export const metadata = {
  title: "Promptverse",
  description: "Discover AI prompts",
};

const MainLayout = ({ children }) => {
  return (
    <html lang="en">
      <body>
        <div className="main">
          <div className="gradient" />
        </div>

        <main className="app">
          <Nav />
          {children}
        </main>
      </body>
    </html>
  );
};

export default MainLayout;
Tiphiid waspOP
oh
what is it
Why do you have an empty div
and a <main> tag
<main> should be in page.tsx
@!=tgt `<main>` should be in page.tsx
Tiphiid waspOP
actually i m learning nextjs
and i was following a yt tutorial, he did use it in layout.tsx
Nuke the empty div
@!=tgt Why do you have an empty div
Tiphiid waspOP
that is empty just make it has a divider
and move <main> to an indivitual page
dont become this guy
@!=tgt dont become this guy
Tiphiid waspOP
lol
@Tiphiid wasp that is empty just make it has a divider
yeah that's shit coding practive
remove it
// "use client";
import Nav from "@components/Nav";
import "@styles/globals.css";
export const metadata = {
  title: "Promptverse",
  description: "Discover AI prompts",
};

const MainLayout = ({ children }) => {
  return (
    <html lang="en">
      <body>
          <Nav />
          {children}
      </body>
    </html>
  );
};

export default MainLayout;

use this as your layout
@Tiphiid wasp Nope even this is not working
try moving Nav to each page
@!=tgt try moving `Nav` to each page
@Tiphiid wasp
@!=tgt <@708500737272840203>
Tiphiid waspOP
nope it didn't work
I know i m doing something wrong, but unable to figure out
@Tiphiid wasp nope it didn't work
thats weird
hold up
try renaming MainLayout to RootLayout
@!=tgt thats weird
Tiphiid waspOP
the weird thing is, if i remove body tag everything is working fine except onClick
@!=tgt try renaming `MainLayout` to `RootLayout`
Tiphiid waspOP
does it matter?
@Tiphiid wasp does it matter?
try
this is a working layout (not gonna work for you since no providers component)
export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html suppressHydrationWarning lang="en">
      <body
        className={`${GeistSans.variable} ${GeistMono.variable} bg-base selection:bg-surface2/60 overflow-scroll`}
      >
        <Providers>{children}</Providers>
      </body>
    </html>
  );
}
@!=tgt try
Tiphiid waspOP
didn't work though
@Tiphiid wasp didn't work though
remove the providers component
and the css classes
@!=tgt remove the providers component
Tiphiid waspOP
yup yup i m not using them
idk then