Next.js Discord

Discord Forum

calling server action from server component

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
Are there any consequences to calling a server action in a react server component.

the reason of having 'use server' is because i need to reuse the function in a client component as well.
Answered by Clown
Its literally how the docs tell you to use it
View full answer

22 Replies

Uh no it shouldn't matter. Thats how you are supposed to use it
Asiatic LionOP
async function ServerComponet() {
  const data = await someServerFn()
}

// _actions.ts
'use server';

export async function someServerFn() { ... }

// client.tsx
'use client';

function Client() {
  const handleEvent = async () => {
    const data = await someServerFn();
  }

  return (...)
}
Asiatic LionOP
yeah i know, but in the case i wasnt using it in a client component im able to just not even use the directive entirely so wasnt sure if there were consequences
someone also said i was doing it wrong and i didnt think it actually mattered :eyes_sus:
Just read those docs man
Its literally how the docs tell you to use it
Answer
So nothing wrong with it
:sunglasses_1:
Read the second point
Its fine if its for reusability
Asiatic LionOP
sure, if i didnt reuse it in client component it wouldnt actually matter right. just clarifying unnecesary 'use server' directives dont cost me anything (at the compiler level etc)
you can also chill on the tone my guy
@Asiatic Lion you can also chill on the tone my guy
I didn't really meant it in the angry way tho
Asiatic LionOP
all good, i didnt think i was doing anything wrong. just needed a sanity check in case I was tripping (pretty sure theyre tripping)
only consequence would be i could inadvertently allow client components to invoke it in which case i should use the server-only directive or not have a directive it all
Asiatic LionOP
yeah im just speaking out loud, in my large codebase another engineer may think they can call ('use server' is kinda like an invitation with no context) it when it might be leaking some data never meant to used client side
anyways, thanks for clarifying
❤️
@Asiatic Lion ❤️
Please mark the answer 🙂