Logout function not liking useRouter
Answered
King Shepherd posted this in #help-forum
King ShepherdOP
My logout function in form.tsx is causing the attached error
Answered by aardani
a server component can't have interactivity. You have to delegate it to a client component
47 Replies
So
where is Signout function being used?
King ShepherdOP
in the guestbook page:
@aardani
So GuestBook is a server component right
a server component can't have interactivity. You have to delegate it to a client component
Answer
King ShepherdOP
guestbook/page.tsx is
the button is a client component
and the function the button is connected to is also a client componenet
No, its not a client component. Its just a function
If you move the <Button> to a file with "use client" only then it will be a client component
King ShepherdOP
thats how it is
allright so there you go
@aardani allright so there you go
King ShepherdOP
I'm confused, the button is part of a client component, and the onClick function is also part of the same client component
Yes that means you don't need to put the SIgnout inside the
onClick callbackjust use
<Signout /> in your Guestbook pageKing ShepherdOP
How do I make the button trigger the fetch call if not with
onClickwith the
onClick you defined inside Signoutyou can only use
onClick in Client componentKing ShepherdOP
so do i have it right then?
well try it out, and tell me if it works. Im not a compiler so don't waste time asking me if it works 🤣
King ShepherdOP
my bad haha
no it does not
where is Signout being used?
oh
you have to define what happen when the button is clicked.
as of now ure just referencing itself
King ShepherdOP
It fires the API fetch right?
you would only want to fetch when the button is clicked right?
then don't put it outside a callback
the way you set it up right now is that the fetch will be instantly called the moment the <SignOut> component is rendered.
King ShepherdOP
ok I see. can I extract it to a async function inside the same file?
@aardani
like this:
jk i cannot
well
thats better
1. put the logout function inside the Signout component
2.
3. put
2.
onClick={async () => await logout()} IF just logout doesn't work3. put
useRouter() in a top-level react component, on the same level as the return() and below Signout(). Really look up the [do's and don'ts of hooks](https://react.dev/warnings/invalid-hook-call-warning)King ShepherdOP
looks like it works! thanks
theres a lucia thing where it isn't invalidating the session but that's beyond the scope of next