Der .isSuccess-Fehler von React Query tritt auf, wenn ich axios verwende, um eine Get-Anfrage zu stellen
P粉153503989
P粉153503989 2023-08-15 23:34:15
0
1
503
<p><br /></p> <pre class="brush:php;toolbar:false;">const searchInvoiceList = async ( plantLoc: Zeichenfolge, RechnungOption: Zeichenfolge ) => { let data: InvoiceData[] = []; warte auf Axios .get(`${linkURL}inv/getControlList/${plantLoc}/${invoiceOption}`) .then((response) => { data = Response.data.sqlData; }) .catch((error) => console.error(error)); Rückgabedaten; }; const fetchInvoices = useQuery({ queryKey: [ "Rechnungsdaten", plantLocationOptionChecked.value, billOptionChecked.value, ], queryFn: () => searchInvoiceList( plantLocationOptionChecked.value, billOptionChecked.value ), aktiviert: falsch, Wiederholung: 0, }); if (fetchInvoices.isSuccess) { if (fetchInvoices.data.length === 0) { toast.error("搜索结果为空"); } setFetchedInvoiceData(fetchInvoices.data); } if (fetchInvoices.isError) { console.log(fetchInvoices.error); }</pre> <p> „Reagieren“ ist nicht möglich <code>setFetchedInvoiceData(fetchInvoices.data);</code> :/code> )“。我做错了什么,如何修复这个问题?</p>
P粉153503989
P粉153503989

Antworte allen(1)
P粉517090748

在没有查看您整个组件的代码的情况下,很难为您提供最佳解决方案,但是目前在这里我看到您在每次重新渲染时都在fetchInvoices.isSuccess上运行条件,解决的最佳方法是将其添加到useEffect中,如下所示:

useEffect(() => {
      if (fetchInvoices.isSuccess) {
        if (fetchInvoices.data.length === 0) {
          toast.error("搜索结果为空");
        }
        setFetchedInvoiceData(fetchInvoices.data);
      }
     if (fetchInvoices.isError) {
       console.log(fetchInvoices.error);
     }
   }, [fetchInvoices.isSuccess, fetchInvoices.isError])
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage