How do I set the default useState value of a trpc call?

EDIT: Sorry apparently I can't rename the title. I meant to say: 'How do I set a default useState value to something that came from a trpc call'

Hi folks, so I need some help with my component

It's a simple team switcher. I need help understanding a few things. I have two main trpc endpoints: getAllForLoggedUser and switchActiveWorkspace.
one thing: whenever I am reloading this component fresh new, I am not being able to set the default value of the useState call. The default value is not being set. How can I make it so it sets the default value that came from the trpc call


export default function TeamSwitcher({ className }: TeamSwitcherProps) {
  const ctx = api.useContext();
  const { data: session } = useSession();

  const { data: workspaces } = api.workspace.getAllForLoggedUser.useQuery(
    undefined,
    {
      enabled: session?.user !== undefined,
    }
  );

  const { mutateAsync } = api.user.switchActiveWorkspace.useMutation({
    onSuccess: () => {
      void ctx.workspace.getAllForLoggedUser.invalidate();
    },
  });

  const [selectedWS, setSelectedWS] = React.useState<Workspace>({
    id: session?.user?.activeWorkspaceId || "",
    name:
      workspaces?.find((w) => w.id === session?.user?.activeWorkspaceId)
        ?.name || "", //This was supposed to be working 🥲
  });

  return (
    {/*tsx Code.
      At some point there is a mapping of the workspaces array and on each item there is a onSelect like this
      onSelect={() => {
                      setSelectedWS({
                        id: ws.id,
                        name: ws.name,
                      });
                      setOpen(false);
                      void mutateAsync({ workspaceId: ws.id });
                    }}
  */}
  )
}
image.png
Was this page helpful?