I am stuck at a point where I am creating a custom button component, marking it as a client component, and then I am passing its onClick callback from a server-side component, and it's giving me this error:
error Error: Event handlers cannot be passed to Client Component props.
<button onClick={function} className=... children=...>
^^^^^^^^^^
If you need interactivity, consider converting part of this to a Client Component.
at stringify (<anonymous>)
digest: "280571554"
This is my WelcomeComponent.tsx file:
import { faPizzaSlice } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Brand, Button } from "@/components/Common";
import Image from "next/image";
const WelcomeComponent = () => {
return (
<div className="border flex items-center justify-center flex-wrap space-x-10 py-10">
<div className="font-semibold flex flex-col">
<div className="mr-3 flex items-center text-3xl">
<span className="mr-2">Welcome to</span>
<Brand size="large" />
</div>
<h2 className="text-2xl">We Serve you with happiness.</h2>
<Button
text="Order Now"
icon={<FontAwesomeIcon className="w-6" icon={faPizzaSlice} />}
onClick={() => {
console.log("c");
}}
/>
</div>
<Image
className="w-auto h-auto"
height={0}
width={300}
src="/man.jpg"
priority
alt=""
/>
</div>
);
};
export default WelcomeComponent;
And this is my Button.tsx file:
interface Props {
text: string;
icon: React.JSX.Element;
onClick: () => void;
}
const Button = ({ text, icon, onClick }: Props) => {
return (
<button
onClick={onClick}
className="shadow-lg my-5 flex items-center justify-center text-white bg-orange-400 hover:bg-orange-500 rounded-md p-3 w-40 space-x-2"
>
<span className="text-md">{text}</span>
{icon}
</button>
);
};
export default Button;
As you can see I want to pass the onClick callback to the Button component which I am calling in the WelcomeComponent.tsx.
I'm using Next.js v13.4.