SolidJSS
SolidJSโ€ข12mo agoโ€ข
2 replies
Rvespula

createResource

I'm trying to be able to reuse functions for API calls, like how it's done with React Query, but the value of data.isLoading loses reactivity when I use it in my component.

/// function
export function useGetUserById(id: string) {
  const [data] = createResource(() => getUsersIdUser(id));
  const [isLoading, setIsLoading] = createSignal(data.loading);

  createEffect(() => {
    setIsLoading(data.loading);
  });

  return {
    isLoading,
    data,
  };
}

// Use:

 const { data, isLoading } = useGetUserById(
    session.userSession.user.id,
  );
Was this page helpful?