Next.js Discord

Discord Forum

Client-side and server-side components question

Answered
Pacific herring posted this in #help-forum
Open in Discord
Pacific herringOP
So basically I am working on a page and needed to redirect my user upon clicking a button somewhere else, so i used an event handler:

  <button onClick={(lid) => router.push("/worldwideweb")}>click here</button>


only to get:
- error Error: Event handlers cannot be passed to Client Component props.

I solved this by creating a whole new client side component, CSButton, and pass do everything there:

'use client'
import { useRouter } from "next/navigation";
import { TCSButton } from "../types";

const CSButton = (params: TCSButton) => {
    const { pushTo, text } = params;

    const router = useRouter();
    return <button onClick={(lid) => router.push(pushTo)}>{text}</button>
}

export default CSButton;


Now i have this folder with clientside components only where i use my hooks and click handlers, though my doubt is:

is it the correct way? at some point i will have duplicated of components that are CS and SS, same thing but one is clientside, one is serverside, but some components need to be SS.
Answered by Blue horntail woodwasp
In my thought, we can only triggred event on CS. If you want to redirect, maybe wrap button inside <Link> from next/link
View full answer

15 Replies

Blue horntail woodwasp
In my thought, we can only triggred event on CS. If you want to redirect, maybe wrap button inside <Link> from next/link
Answer
@Blue horntail woodwasp In my thought, we can only triggred event on CS. If you want to redirect, maybe wrap button inside `<Link>` from `next/link`
Pacific herringOP
// SERVER SIDE COMPONENT 
    <Link href="/dashboard" replace>
       <button onClick={(lid) => router.push("/worldwideweb")}>click here</button>
    </Link>
this way u mean?
Blue horntail woodwasp
maybe just remove the on click
I dont know if its will work. but in my opinion, thats the problem
Pacific herringOP
yh i forgot to remove on click from the old example
Remix and now Next would call that "using the platform", with RSC and App Router the idea is to try to first use basic HTML APIs, like links, and then using interactive JS logic (event handlers, stateful React component) only if needed
sometimes, your app needs JS everywhere anyway
so it's just a good practice rather than a rule or what
The limitation of onClick is that you lose proper browser interaction like right click to open in new tab
?
yes what @Blue horntail woodwasp proposed is perfect
Pacific herringOP
Thanks @Eric Burel @Blue horntail woodwasp
this client side then server side thing sometimes gets confusing as hell