Next.js Discord

Discord Forum

using a server action in a client component

Answered
Nile Crocodile posted this in #help-forum
Open in Discord
Nile CrocodileOP
hey, so im trying to use a server action in a client component and it's erroring saying that i cant use await/async in a client component. the thing is i did use async/await in one of my other client components which is working fine but this one is erroring. all im doing is calling a function on an input change event. any help would be appreciated, ty!
Answered by Ray
hi try this
async function handleUserSearch(username: string) {
  if (username === "") {
    startTransition(() => {
      setUsers([]);
    });
  }
  const users = await getUsersByUsername(username, 50);
  startTransition(() => {
    setUsers(users);
  });
}

as the doc said, the callback shouldn't be async in useTransition
https://react.dev/reference/react/useTransition#react-doesnt-treat-my-state-update-as-a-transition
View full answer

2 Replies