'use server' within server components #68505
Unanswered
Nicky-Sa
asked this question in
App Router
Replies: 1 comment 4 replies
-
|
@Nicky-Sa Hello. I think you can use the second approach when you don't want to move the server function to another module and when you don't want to import the server function directly from a client component. import { Button } from './button';
import { Link } from './link';
interface Props {
searchParams: {
filter: string;
};
}
export default function Page({ searchParams: { filter } }: Props) {
async function doSomeAction() {
'use server';
console.log('do something');
}
if (filter) {
return <Link onClick={doSomeAction}>Click me</Link>;
}
return <Button onClick={doSomeAction}>Click me</Button>;
}It's nice that you don't have to create a wrapper for the link and button every time you need to import a server action when you want to pass server actions to client components. Does it make sense? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone. I have a question about Next.js "use server" directive.
I know that we can have server actions in 2 ways:
What I don't understand is in what situation we might need to use the second way? If we're in a component, either we want to get or post data. In server components, if we're getting data, we just use the plain fetch with async/await pattern, no server action needed. Also, in server components, we can't have user interaction, so we can't post data based on user input. Again, no need for server action!
Then when can we declare a function to run on server? Isn't the component already running on server?
Beta Was this translation helpful? Give feedback.
All reactions