senchabot-opensource / monorepo

Open source Discord bot and Twitch bot monorepo. Senchabot apps and packages.
https://senchabot.app
GNU General Public License v3.0
84 stars 23 forks source link

feat(apps/web): perform data fetching with useEffect with useQuery #412

Closed muhammed-gumus closed 8 months ago

muhammed-gumus commented 9 months ago

Data fetching was done with useEffect and useQuery.

Before:

 const [isLoading, setIsLoading] = useState<boolean>(true);
  const [accounts, setAccounts] = useState<IAccount[]>([]);

  useEffect(() => {
    getAccount().then(res => {
      if (!res.data) return;
      setAccounts(res.data);
      setIsLoading(false);
    });
  }, [isLoading]);
  const currentProviders = accounts?.data?.map(
    (account: IAccount) => account.provider,
  );

After:

 const { data: accounts, isLoading } = useQuery({
    queryKey: ["accounts"],
    queryFn: () => {
      return getAccount();
    },
  });
  const currentProviders = accounts?.data?.map(
    (account: IAccount) => account.provider,
  );