Next.js Discord

Discord Forum

Server actions

French Angora posted this in #help-forum
Open in Discord
French AngoraOP
I have question regarding using server actions in a client component. The docs state that we should use the startTransition hook when we want to run a server action from client. I tried something a big different and it seem to work but not sure if this is the correct behavior how server action should be used
I have a client component:
"use client"
function ScenePanel() {
  const { data, setData, controllers } = useDynamicTree<NodeItem>();

  useEffect(() => {
    const getData = async () => {
      const scenes = await fetchScenes();
  }, []);
  return <div></div>

Then I have a file where I keep all of my server actions:
"use server";

export async function fetchScenes() {
  // This function would make an api call using the fetch method
  const scenes = ["a", "b", "c"];

  return scenes;

The following works without issues that I can see. I would like to know if this is the correct usage of client component using a server action. OR would this cause wierd behavor or expose my api to the client?

4 Replies

European sprat
this looks like normal usage of server actions but as far as exposing the api client, server actions are basically POST requests (from what i understand) under the hood so a user could see some details related to that but if you're talking about exposing secrets in function used in the action, it should be ok
French AngoraOP
cool. one last question when should we use the useTransition hook
European sprat
not sure