Heim  >  Fragen und Antworten  >  Hauptteil

Der .isSuccess-Fehler von React Query tritt auf, wenn ich axios verwende, um eine Get-Anfrage zu stellen

<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粉153503989P粉153503989402 Tage vor411

Antworte allen(1)Ich werde antworten

  • P粉517090748

    P粉5170907482023-08-16 18:54:18

    在没有查看您整个组件的代码的情况下,很难为您提供最佳解决方案,但是目前在这里我看到您在每次重新渲染时都在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])

    Antwort
    0
  • StornierenAntwort