Der .isSuccess-Fehler von React Query tritt auf, wenn ich axios verwende, um eine Get-Anfrage zu stellen
P粉153503989
2023-08-15 23:34:15
<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>
在没有查看您整个组件的代码的情况下,很难为您提供最佳解决方案,但是目前在这里我看到您在每次重新渲染时都在
fetchInvoices.isSuccess
上运行条件,解决的最佳方法是将其添加到useEffect
中,如下所示: