我有两个功能。第一个获取数据并返回所有引号、突变函数和加载布尔值。
// Fetcher and the baseUrl
export const baseURL = 'http://localhost:3000/api/';
export const fetcher = (arg: any, ...args: any) => fetch(arg, ...args).then(res => res.json());
//get completed quotes
export const getAllQuotesArray = () => {
const { data: allQuotes, isLoading: allQuotesLoading, mutate: mutateAllQuotes } =
useSWR(`${baseURL}/get-quotes`, fetcher);
return { allQuotes, allQuotesLoading, mutateAllQuotes };
}
第二个函数删除一条记录并返回剩余的对象数组(与API路由中相同)。
//get completed quotes
export const getCompletedQuotes = async () => {
try {
const response = await fetch(`${baseURL}get-completed-quotes`, {
method: 'GET'
});
const data = await response.json();
return data;
} catch (error) {
console.log('', error);
}
}
export const deleteFromCompletedQuotes = async (quoteId: string) => {
try {
await fetch(`${baseURL}delete-quote?id=${quoteId}`, {
method: 'DELETE'
});
const allRemainingCompletedQuotes = await getCompletedQuotes();
//re run the function
getAllQuotesArray();
//return an array of objects
return allRemainingCompletedQuotes.completedQuotes;
} catch (error) {
console.log('', error);
}
}
如何重新验证deleteFromCompletedQuotes
函数中的数据?我试过很多次变异...但这行不通。我的方法的原因是因为我有两个组件在我的网页,其中一个显示完整的报价和其他所有的报价。但是如果我删除一条记录,在第二个div中不会发生任何变化...
1条答案
按热度按时间js5cn81o1#
getAllQuotesArray()
返回{ allQuotes, allQuotesLoading, mutateAllQuotes }
。可以在deleteFromCompletedQuotes
中使用mutateAllQuotes
mutatorexport const deleteFromCompletedQuotes = async(quoteId:string)=> { try { await fetch(
${baseURL}delete-quote?id=${quoteId}
,{ method:"}); const allRemainingCompletedQuotes = await getCompletedQuotes();}